Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash: Multiple Lines from File Into One Command Argument

Tags:

bash

shell

I need to pass multiple lines from one file into a script as one comma separated argument. Whenever I try to use the output of processing the file as a single string, the commas become separators. How can I do this?

Test case:

[user@host]$ #Here is a word list, my target words are on lines starting with "1,"
[user@host]$ cat word_list_numbered.txt
1,lakin
2,chesterfield
3,sparkplug
4,unscrawling
5,sukkah
1,girding
2,gripeful
3,historied
4,hypoglossal
5,nonmathematician
1,instructorship
2,loller
3,containerized
4,duodecimally
5,oligocythemia
1,nonsegregation
2,expecter
3,enterrologist
4,tromometry
5,salvia
[user@host]$ #Here is a mock operation, it just outputs the number of args, I want all selected words as one argument
[user@host]$ cat operation.sh
echo "This script has $# arguments"
[user@host]$ #Here is a script that outputs the needed words as comma delimited
[user@host]$ grep '^1,' word_list_numbered.txt | tr -d '1,' | tr '\n' ',' | sed 's/,$//'        lakin,girding,instructorship,nonsegregation[user@host]$
[user@host]$ #Here is the operation script receiving that comma delimited list
[user@host]$ ./operation.sh $(grep '^1,' word_list_numbered.txt | tr -d '1,' | tr '\n' ',' | sed 's/,$//')
This script has 4 arguments
[user@host]$ #oops, too many arguments
[user@host]$ ./operation.sh foo,bar
This script has 1 arguments
[user@host]$ ./operation.sh foo bar
This script has 2 arguments
[user@host]$

Details:

  • The needed words are in lines starting with 1,
  • All words should be passed to operation.sh as one comma-delimited argument
  • I don't have control over the format of word_list_numbered.txt or the need for operation.sh to take all words as one comma-delimited argument
  • It is not optimal to run operation.sh many times--I'm asking this question so I don't have to do that
like image 424
Adolph Trudeau Avatar asked May 06 '26 01:05

Adolph Trudeau


2 Answers

How about a combination of awk and xargs?

 awk -F, -v ORS=, '$1==1{print $2}' file | xargs ./operation.sh

Or if you mind the trailing comma:

 awk -F, -v ORS=, '$1==1{print $2}' file | sed 's/,$//' | xargs ./operation.sh

Test:

$ cat operation.sh 
echo "This script has $# arguments"
echo "$@"

$ awk -F, -v ORS=, '$1==1{print $2}' file | sed 's/,$//' | xargs ./operation.sh 
This script has 1 arguments
lakin,girding,instructorship,nonsegregation

$ cat file
1,lakin
2,chesterfield
3,sparkplug
4,unscrawling
5,sukkah
1,girding
2,gripeful
3,historied
4,hypoglossal
5,nonmathematician
1,instructorship
2,loller
3,containerized
4,duodecimally
5,oligocythemia
1,nonsegregation
2,expecter
3,enterrologist
4,tromometry
5,salvia

Without xargs it would be:

./operation.sh "$(awk -F, -v ORS=, '$1==1{print $2}' file | sed 's/,$//')"
like image 50
user000001 Avatar answered May 08 '26 13:05

user000001


Given:

$ echo "$tgt"
1,lakin
2,chesterfield
3,sparkplug
4,unscrawling
5,sukkah
1,girding
2,gripeful
3,historied
4,hypoglossal
5,nonmathematician
1,instructorship
2,loller
3,containerized
4,duodecimally
5,oligocythemia
1,nonsegregation
2,expecter
3,enterrologist
4,tromometry
5,salvia

In Perl:

$ echo "$tgt" | perl -F',' -lane '$A[++$#A]=$F[1] if $F[0]=="1"; END{ print join(",", @A) }'
lakin,girding,instructorship,nonsegregation
like image 24
dawg Avatar answered May 08 '26 13:05

dawg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!