Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to process every other line in bash

Tags:

bash

awk

I would like to print odd lines (1,3,5,7..) without any change, but even lines (2,4,6,8) process with pipeline beginning with grep. I would like to write everything to new file (odd lines without any change and new values for even lines).

I know how to print every other line in awk:

awk ' NR % 2 == 1 { print; } NR % 2 ==0 {print; }' file.fasta

However, for even lines, I dont want to use {print; } but I want to use my grep pipeline instead.

An advice will be appreciated. Thanks a lot.

like image 414
Perlnika Avatar asked Jul 19 '12 12:07

Perlnika


People also ask

How do I print every second line in Linux?

Using sed:n command in sed prints the current line(if -n is not present) and reads the next line in the buffer. Since -n switch is present, the n command does not print.

How do you print alternate lines in Unix?

Print every alternate line:n command prints the current line, and immediately reads the next line into pattern space. d command deletes the line present in pattern space. In this way, alternate lines get printed.

What is ${ 2 in bash?

It means "Use the second argument if the first is undefined or empty, else use the first". The form "${2-${1}}" (no ':') means "Use the second if the first is not defined (but if the first is defined as empty, use it)". Copy link CC BY-SA 2.5.

What is “for each line in file” in Bash?

One such variation is the “For each line in file” which is responsible for reading all the lines in a file. In this article, we will talk about the methods of using “for each line in file” in Bash. Note: The methods shown below have been demonstrated on Ubuntu 20.04. However, they will also work well with any other Linux distribution.

How to process every line in a text file in Linux?

Solution: A simple way to process every line in a text file is to use a Unix/Linux while loop in combination with the Linux cat command, like this: Now all you have to do is put some code inside the while loop.

How to copy read lines from BASH script to text file?

Now run the Bash script again via the terminal with the bash command followed by the Bash file name. This time when the Bash script will run, it will create a new text file whose name in this case is NewFile.txt. The output shown in the image below will assure you that all the read lines have been copied to the new text file.

How to display all lines of a bash script in Linux?

Then in the “do-done” block, we have simply displayed all these lines on the terminal by making use of the echo command. When this command will make the bash script to execute, then you will be able to see all the lines of your text file on your terminal as shown in the following image:


1 Answers

If you're planning to do a simple grep, you can do away with the additional step and do the filtering within awk itself, e.g.:

awk 'NR % 2 {print} !(NR % 2) && /pattern/ {print}' file.fasta

However, if you intend to do a lot more then, as chepner already pointed out, you can indeed pipe from inside awk. For example:

awk 'NR % 2 {print} !(NR % 2) {print | "grep pattern | rev" }' file.fasta

That opens a pipe to the command "grep pattern | rev" (note the surrounding quotes) and redirects the print output to it. Do note that the output in this case may not be as you might expect; you will end up with all odd lines being output first followed by the output of the piped command (which consumes the even lines).


(In response to your comments) to count the number of chars in each even line, try:

awk 'NR % 2 {print} !(NR % 2) {print length($0)}' file.fasta
like image 98
Shawn Chin Avatar answered Oct 04 '22 02:10

Shawn Chin