Skip to content
Advertisement

redirect and print script output using for loop and awk in linux

I have a script which give out put like this :

my_script <variables>

output like this :

    text1   text3     textext3   texxt6   textxx2
    textx3  textext3  textext1   text4    text4
    text1x  textx2    text5      textxt0  textxx
    ..........
    ..........
    .......... so on

now I want to redirect the output and print like this

print “some_text col_3@col_4 some_more_text col_4

ans will be like this:

some_text textext3@texxt6 some_more_text texxt6

some_text textext1@text4 some_more_text text4

some_text text5@textxt0 some_more_text textxt0

….. so on

col_3 = all values in column 3, col_4 = all values in column 4

what is the simple way to print like this using for loop and awk NOTE : There is no correlation between all fields and length is not fixed

Advertisement

Answer

AWK solution:

awk '{ print "some_text" OFS $3 "@" $4 OFS "some_more_text" OFS $4 }' YOURFILE

Or in your case:

your_script <variables> | awk '{ print "some_text" OFS $3 "@" $4 OFS "some_more_text" OFS $4 }'

Example input:

    text1   text3     textext3   texxt6   textxx2
    textx3  textext3  textext1   text4    text4
    text1x  textx2    text5      textxt0  textxx

Example output:

some_text textext3@texxt6 some_more_text texxt6
some_text textext1@text4 some_more_text text4
some_text text5@textxt0 some_more_text textxt0
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement