Remove last character of every nth line in place

Cody Glickman

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.

dawg

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.

edited at
0

Comments

0 comments
Login to comment

Related

How to remove last character of Nth line linux

Remove last character on each line

Remove the Nth specified character from a line

How can I remove every nth character from a string in javascript?

Notepad++ insert new line at every nth occurrence of a string/character

Replace every nth instance of a character with a new line in Notepad++

Bash, sed, remove last line for every cycle

Bash - Remove the last character of the line this before?

remove character on the last line that specific word appears

remove character on the last line that specific word appears

Shell: Remove last character of line if it is double quote

shell script to remove last character of a line

Remove Last Character of Each Line that Starts With @

removing the first and the last character of every line from command line

sed: Remove from nth character until crossing keyword in each line

How to replace last character on every line Notepad++

Notepad++ Insert a character before the last three characters in every line

awk: How to add a constant to a number every Nth line out of M lines in place?

How do I remove every second character in all line with Emeditor

Add text on every nth line

Delete every Nth line in shell

How to trim every nth line?

Remove everything after the last occurrence of selected character in the first line

sed-remove all but last occurrence of a character from a line

Remove line if the second or second to last character is a space in the first column of a CSV

How to remove nth character in string?

Remove nth character from string

Sed delete line if nth character is not specific character

How to select (remove) every 6th (Nth) line in a text file?