How to accept multiple arguments in bash and pass them awk?

I am writing a function in which I replace the leading / trailing space from a column, and if there is no value in the column, replace it with zero. The function works fine for a single column, but how can I change it for multiple columns.

Function:

#cat trimfunction #!/bin/bash function trim { vCol=$1 ###input column name vFile=$2 ###input file name var3=/home/vipin/temp ###temp file awk -v col="${vCol}" -f /home/vipin/colf.awk ${vFile} > $var3 ###operation mv -f $var3 $vFile ###Forcefully mv } 

AWK script:

 #cat colf.awk #!/bin/awk -f BEGIN{FS=OFS="|"}{ gsub(/^[ \t]+|[ \t]+$/, "", $col) ###replace space from 2nd column } {if ($col=="") {print $1,"NULL",$3} else print $0} ###replace whitespace with NULL 

Input file: leading / trailing / space in the second column

 #cat filename.txt 1| 2016-01|00000321|12 2|2016-02 |000000432|13 3|2017-03 |000004312|54 4| |000005|32 5|2017-05|00000543|12 

Script:

 #cat script.sh . /home/vipin/trimfunction trim 2 filename.txt 

Output file: master / trailing / white space deleted in second column

 #./script.sh #cat filename.txt 1|2016-01|00000321|12 2|2016-02|000000432|13 3|2017-03|000004312|54 4|NULL|000005 5|2017-05|00000543|12 

If the input file is similar to the one below - (white / lead / trading space in the second and fifth column of the file)

 1|2016-01|00000321|12|2016-01 |00000 2|2016-02 |000000432|13| 2016-01|00000 3| 2017-03|000004312|54| |00000 4| |000005|2016-02|0000 5|2017-05 |00000543|12|2016-02 |0000 

How to achieve the result below - (all leading / trailing space is truncated and the space is replaced by NULL in the 2nd and 5th columns) something like trimming 2 5 filename.txt trim 2 5 filename.txt ###, passing two names column as input

 1|2016-01|00000321|12|2016-01|00000 2|2016-02|000000432|13|2016-01|00000 3|2017-03|000004312|54|NULL|00000 4|NULL|000005|2016-02|0000 5|2017-05|00000543|12|2016-02|0000 
+5
source share
3 answers

This will do what you said you want:

 $ cat tst.sh file="${!#}" cols=( " $@ " ) unset cols[$(( $# - 1 ))] awk -v cols="${cols[*]}" ' BEGIN { split(cols,c) FS=OFS="|" } { for (i in c) { gsub(/^[[:space:]]+|[[:space:]]+$/,"",$(c[i])) sub(/^$/,"NULL",$(c[i])) } print }' "$file" $ ./tst.sh 2 5 file 1|2016-01|00000321|12|2016-01|00000 2|2016-02|000000432|13|2016-01|00000 3|2017-03|000004312|54|NULL|00000 4|NULL|000005|2016-02|0000 5|2017-05|00000543|12|2016-02|0000 

but if what you REALLY wanted was to work on ALL fields instead of specific ones, then, of course, there was a simpler solution.

Never do cmd file > tmp; mv tmp file cmd file > tmp; mv tmp file way, always do cmd file > tmp && mv tmp file instead (pay attention to && ) so that you only overwrite the original file if the command is successful. Also, always specify your shell variables if you do not have a very specific goal, without doing this and not fully understanding all the consequences, so use "$file" , not $file . Google it.

+4
source

You can pass a list of columns to change as a parameter. File creation

 $ cat trim.awk BEGIN { split(c, a) FS = OFS = "|" } { for (i in a) { i = a[i] gsub(/^[ \t]+|[ \t]+$/, "", $i) if (!length($i)) $i = "NULL" } print } 

and

 $ cat filename.txt 1|2016-01|00000321|12|2016-01 |00000 2|2016-02 |000000432|13| 2016-01|00000 3| 2017-03|000004312|54| |00000 4| |000005|2016-02|0000 5|2017-05 |00000543|12|2016-02 |0000 

Using:

 awk -vc="2 5" -f trim.awk filename.txt 
+2
source

If managing leading / trailing spaces is all you want to do, you probably don't want to do everything (AWK code), which is.

cat q1.txt | tr -s ' ' | sed 's/|\ |/|NULL|/g' | sed 's/\ //g' cat q1.txt | tr -s ' ' | sed 's/|\ |/|NULL|/g' | sed 's/\ //g' .

Breakdown
tr -s ' ' : Compress multiple spaces into one
sed 's/|\ |/|NULL|/g' : Replace all with "| |" with "| NULL |"
sed 's/\ //g' : Replace all spaces with an empty string.

+1
source

Source: https://habr.com/ru/post/1259589/


All Articles