How to pass variables to awk command line


username

I'm having some trouble passing a bash script variable to the awk command line.

Here is pseudo code:

for FILE in $INPUT_DIR/*.txt; do
 filename=`echo $FILE | sed -n 's/^.*\(chr[0-9A-Z]*\).*.vcf$/\1/p'`
 OUTPUT_FILE=$OUTPUT_DIR/$filename.snps.txt
 egrep -v "^#" $FILE | awk '{print $2,$4,$5}' > $OUTPUT_FILE
done

In the row of my last column, I want it to be flexible or user input. For example, the user might also want columns 6, 7, and 8, or columns 133 and 138, or columns 245 to 248. So how can I customize it so I can get 'print $2....$5' what the user entered? For example, the user will run the following script: bash script.sh input_dir output_dir [user enters any column string] and I will get those columns in the output. I tried passing it, but I guess the syntax is not correct.

Michal Krzyz

The following test worked for me:

A="\$3" ; ls -l | awk "{ print $A }"

Related


How to pass variables to awk command line

username I'm having some trouble passing a bash script variable to the awk command line. Here is pseudo code: for FILE in $INPUT_DIR/*.txt; do filename=`echo $FILE | sed -n 's/^.*\(chr[0-9A-Z]*\).*.vcf$/\1/p'` OUTPUT_FILE=$OUTPUT_DIR/$filename.snps.txt egre

How to pass variables to awk command line

username I'm having some trouble passing a bash script variable to the awk command line. Here is pseudo code: for FILE in $INPUT_DIR/*.txt; do filename=`echo $FILE | sed -n 's/^.*\(chr[0-9A-Z]*\).*.vcf$/\1/p'` OUTPUT_FILE=$OUTPUT_DIR/$filename.snps.txt egre

How to pass variables to awk command line

username I'm having some trouble passing a bash script variable to the awk command line. Here is pseudo code: for FILE in $INPUT_DIR/*.txt; do filename=`echo $FILE | sed -n 's/^.*\(chr[0-9A-Z]*\).*.vcf$/\1/p'` OUTPUT_FILE=$OUTPUT_DIR/$filename.snps.txt egre

How to pass variables to awk command line

username I'm having some trouble passing a bash script variable to the awk command line. Here is pseudo code: for FILE in $INPUT_DIR/*.txt; do filename=`echo $FILE | sed -n 's/^.*\(chr[0-9A-Z]*\).*.vcf$/\1/p'` OUTPUT_FILE=$OUTPUT_DIR/$filename.snps.txt egre

How to pass variables to awk command line

username I'm having some trouble passing a bash script variable to the awk command line. Here is pseudo code: for FILE in $INPUT_DIR/*.txt; do filename=`echo $FILE | sed -n 's/^.*\(chr[0-9A-Z]*\).*.vcf$/\1/p'` OUTPUT_FILE=$OUTPUT_DIR/$filename.snps.txt egre

How to pass variables from awk to shell command?

Vahid Mirjalili I'm trying to run a shell command from awk for each line of a file that requires an input parameter. I tried to use system()but the input parameter is not recognized. Each line of this file is the address of a file that I want to run a command

How to pass variables from awk to shell command?

Vahid Mirjalili I'm trying to run a shell command from awk for each line of a file that requires an input parameter. I tried to use system()but the input parameter is not recognized. Each line of this file is the address of a file that I want to run a command

How to pass command line arguments in awk script

Masib This type of question has been asked, but not exactly. I am new to bash and awk scripting. So I don't know how to achieve my goal. My goal is to generate sql inserts from csv files. Here is my csv file content: ID;NAME;AGE;TITLE;DATE;SALARY 1;Test1;36;En

How to pass command line arguments in awk script

Masib This type of question has been asked, but not exactly. I am new to bash and awk scripting. So I don't know how to achieve my goal. My goal is to generate sql inserts from csv files. Here is my csv file content: ID;NAME;AGE;TITLE;DATE;SALARY 1;Test1;36;En

How to pass command line arguments in awk script

Masib This type of question has been asked, but not exactly. I am new to bash and awk scripting. So I don't know how to achieve my goal. My goal is to generate sql inserts from csv files. Here is my csv file content: ID;NAME;AGE;TITLE;DATE;SALARY 1;Test1;36;En

How to pass command line arguments in awk script

Masib This type of question has been asked, but not exactly. I am new to bash and awk scripting. So I don't know how to achieve my goal. My goal is to generate sql inserts from csv files. Here is my csv file content: ID;NAME;AGE;TITLE;DATE;SALARY 1;Test1;36;En

How to pass command line arguments in awk script

Masib This type of question has been asked, but not exactly. I am new to bash and awk scripting. So I don't know how to achieve my goal. My goal is to generate sql inserts from csv files. Here is my csv file content: ID;NAME;AGE;TITLE;DATE;SALARY 1;Test1;36;En

How to pass command line arguments in awk script

Masib This type of question has been asked, but not exactly. I am new to bash and awk scripting. So I don't know how to achieve my goal. My goal is to generate sql inserts from csv files. Here is my csv file content: ID;NAME;AGE;TITLE;DATE;SALARY 1;Test1;36;En

How to pass variables as command line arguments in iPython?

Frederica For example, I have a Python module and an iPython module, cmd.py import sys print sys.argv test.ipynb for i in range(5): %run cmd.py i When I run test.ipynb, it prints ['cmd.py', 'i'] multiple times. How can I pass the value of i instead of "i"

How to pass variables to command line programs?

Sindhu S I want to create the file with the current date as a prefix and some string as the rest of the filename. like this: touch `date +"%Y-%m-%d-"$1`.md hello $1where should hellocreate a file named 2014-3-3-Hello.md. cmaster - restore monica You can use c

How to pass variables as command line arguments in iPython?

Frederica For example, I have a Python module and an iPython module, cmd.py import sys print sys.argv test.ipynb for i in range(5): %run cmd.py i When I run test.ipynb, it prints ['cmd.py', 'i'] multiple times. How can I pass the value of i instead of "i"

How to pass variables as command line arguments in iPython?

Frederica For example, I have a Python module and an iPython module, cmd.py import sys print sys.argv test.ipynb for i in range(5): %run cmd.py i When I run test.ipynb, it prints ['cmd.py', 'i'] multiple times. How can I pass the value of i instead of "i"

How to pass variables as command line arguments in iPython?

Frederica For example, I have a Python module and an iPython module, cmd.py import sys print sys.argv test.ipynb for i in range(5): %run cmd.py i When I run test.ipynb, it prints ['cmd.py', 'i'] multiple times. How can I pass the value of i instead of "i"

How to pass variables to command line programs?

Sindhu S I want to create the file with the current date as a prefix and some string as the rest of the filename. like this: touch `date +"%Y-%m-%d-"$1`.md hello $1where should hellocreate a file named 2014-3-3-Hello.md. cmaster - restore monica You can use c

How to pass variables to command line programs?

Sindhu S I want to create the file with the current date as a prefix and some string as the rest of the filename. Something like this: touch `date +"%Y-%m-%d-"$1`.md hello $1where should hellocreate a file named 2014-3-3-Hello.md. cmaster - restore monica You

How to pass variables as command line arguments in iPython?

Frederica For example, I have a Python module and an iPython module, cmd.py import sys print sys.argv test.ipynb for i in range(5): %run cmd.py i When I run test.ipynb, it prints ['cmd.py', 'i'] multiple times. How can I pass the value of i instead of "i"

How to pass variables as command line arguments in iPython?

Frederica For example, I have a Python module and an iPython module, cmd.py import sys print sys.argv test.ipynb for i in range(5): %run cmd.py i When I run test.ipynb, it prints ['cmd.py', 'i'] multiple times. How can I pass the value of i instead of "i"

How to pass variables to command line programs?

Sindhu S I want to create the file with the current date as a prefix and some string as the rest of the filename. Something like this: touch `date +"%Y-%m-%d-"$1`.md hello $1where should hellocreate a file named 2014-3-3-Hello.md. cmaster - restore monica You

Pass command line arguments to awk

Fit I'm trying to make a simple bash script that takes command line arguments as arguments to awk. The file format is as follows: id|Name|birthday|joinDate and command line input: ./my_script -f filename.dat -id 1234 When I use the following line of code: aw

How to pass variables to CMake Toolchain file from command line?

Sergey Is it possible to pass variables to the toolchain file when calling cmake? For example, I have the following toolchain files: message("FOO = ${FOO}") I tried the following but it didn't work. This variable is not set in the toolchain file. cmake <src-d