6

When running a command, I get 10 lines of output. I want to take lines 2-4-6-8-10 and put them into an array.

Every time I run my command, the order changes so I need to do this in one go. I had tried running my command and picking out line 2, then running again and picking out line 4 etc., but because the order changes this does not work:

value1=$(my_command |sed '2q;d')
value2=$(my_command |sed '4q;d')
value3=$(my_command |sed '6q;d')
value4=$(my_command |sed '8q;d')
value5=$(my_command |sed '10q;d')

MY_ARRAY=("${value1}" "${value2}" "${value3}" "${value4}" "${value5}")
  • 2
    So is your question about how to read command output into an array - or about how to filter alternate lines of output? Which shell are you using? – steeldriver Sep 19 at 12:52
  • sorry, its is a bash script. My questions is how do i read every other line of command output into an array. – a.smith Sep 19 at 13:54
12

Using readarray in the bash shell, and GNU sed:

readarray -t my_array < <( my_command | sed '1~2d' )

The built-in readarray reads the lines into an array. The lines are read from a process substitution. The sed command in the process substitution will only output every second line read from my_command (and could also be written sed '1!n;d', or as sed -n 'n;p' with standard sed).

In GNU sed, the address n~m addresses every m:th line starting at line n. This is a GNU extension to standard sed, for convenience.

The my_command command will only ever be called once.

Testing:

$ readarray -t my_array < <( seq 10 | sed '1~2d' )
$ printf '%s\n' "${my_array[@]}"
2
4
6
8
10
  • I am using creating a bash script that requires this code. I cant get past a syntax error near unexpected token `<' my code is: readarray -t MY_ARRAY < < ( my_command | sed -n '2~2p') on cmd line my_command | sed -n '2~2p' works as i would hope. – a.smith Sep 19 at 14:06
  • @a.smith Make sure that <(...) does not have a space between < and the (. – Kusalananda Sep 19 at 14:16
  • I am still getting the same error. readarray -t MY_ARRAY < <(echo file.txt | sed -n '2~2p') my command is not an echo of a file but trying to add more context. – a.smith Sep 19 at 14:19
  • the shebang at the top of my scrip is #!/bin/bash but i execute my script with sh my_script.sh - im not to sure of the in's and out's here but i think the problem is im executing with sh. is there an equivalent for sh ? – a.smith Sep 19 at 14:28
  • 2
    @a.smith When you use sh my_script.sh, the shebang line is not having any effect. If your script uses bash features that sh does not understand, such as process substitutions, then don't use sh to execute the script. Use bash, or make your script executable (chmod +x my_script.sh) and run it without an explicit interpreter (./my_script.sh). – Kusalananda Sep 19 at 15:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.