I have a file with four repeated lines. I am looking to remove the last character of every fourth line. A description of the file is below.
@Header
DNA Sequence
+
Quality score!
<Pattern of four above lines repeats>
I am trying to remove the last character (an exclamation point) from every fourth Quality score line.
@Header
DNA Sequence
+
Quality score
<Pattern of four above lines repeats>
I am able to use awk to pull out every fourth line, but how do I remove the last character in place on every fourth line of the file?
This question operates only on a specific line. Currently my approach is to use awk to pull the Quality score and I can remove the last character with sed.
awk 'NR == 4 || NR % 4 == 0'
sed 's/.$//'
I am currently not sure how to overwrite the edited Quality scores into the original file. Any thoughts or more concise inplace sed / awk arguments would be appreciated.
Given:
$ cat file
1!
2!
3!
4!
5!
6!
7!
8!
9!
10!
11!
12!
You can use awk:
$ awk 'NR%4==0{sub(/!$/,"")}1' file
1!
2!
3!
4
5!
6!
7!
8
9!
10!
11!
12
And if you have gawk
you can change in place:
$ gawk -i inplace 'NR%4==0{sub(/!$/,"")}1' file
$ cat file
1!
2!
3!
4
5!
6!
7!
8
9!
10!
11!
12
If you only have POSIX awk, you can effectively get an inplace replacement by using a temp file:
$ awk 'NR%4==0{sub(/!$/,"")}1' file >tmp_file && mv tmp_file file
(Which is what GNU sed or GNU awk or perl or ruby is doing under the covers anyway with 'inplace' replacement...)
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments