Shell Script - "split" em for

Asked

Viewed 191 times

1

I made a query that imputes the result in a file .txt in this mold:

-----id--------action    
H000tg6b JOB
H000oi8a JOB
H00067w JOB

I need to make a for to go through that file and use the two pieces of information.

The first is to use in an operation the second is to check if the action in question is equal to JOB, if it is, execute one command and if it is not, execute another.

The problem is that I’m unable to do is to separate these two attributes by going through the .txt.

I thought about using awk in order to have access to the two attributes in question but I could not continue.

1 answer

1


First you can use tail to ignore the first line (the header). Using the parameter -n +X, being X the line from which you want to consider. Whereas the file is:

-----id--------action
H000tg6b JOB
H000oi8a JOB
H00067w JOB

If I do:

tail -n +2 arq.txt

The exit is:

H000tg6b JOB
H000oi8a JOB
H00067w JOB

Now just pass this exit to the awk and test whether the second field is "JOB":

tail -n +2 arq.txt | awk '{if ($2 == "JOB") { system("ls") } }'

In the example above, I am calling the command ls, but you can trade for whatever you need.


Use system can be a bit inconvenient if you want to call multiple commands. So another alternative is to create a script file:

#!/bin/bash

IFS=$'\n' # o for considera uma linha inteira a cada iteração
for i in `tail -n +2 arq.txt | awk '{ print $1 " " $2 }'`
do
    primeiro=`echo $i | cut -f1 -d' '`
    segundo=`echo $i | cut -f2 -d' '`
    if [ "$segundo" = "JOB" ]; then
        echo "primeiro campo=$primeiro"
        # coloque quantos comandos precisar aqui
    fi
done

As in this example all lines have "JOB" in the second field, the script output will be:

primeiro campo=H000tg6b
primeiro campo=H000oi8a
primeiro campo=H00067w

I used awk because it already separates by several spaces, while the cut only accepts a space as a separator (although it is possible to circumvent this). That’s why I use the awk to format each row with the fields separated by a space, and then use the cut to pick up each field individually.


If the file has no header and you want to get all the lines, change the line of the for for:

for i in `awk '{ print $1 " " $2 }' arq.txt`

Another alternative is to use read (considering a file without the first row of the header). The advantage is that you do not need to use cut to break the line into two variables, you can pass them directly after the read:

#!/bin/bash

(while read primeiro segundo
 do
    if [ "$segundo" = "JOB" ]; then
        echo "primeiro campo=$primeiro"
        # coloque todos os comandos que precisar
    fi
 done ) < <(cat arq.txt)
  • "--------id----action" was just to represent that there are two parameters, ignore them, the original . txt is just the information. In this case I would only use for and awk to collect the two parameters? Could I give an example in this case?

  • @Pedrobaraldini Yes, just do it awk '{ print $1 " " $2 }' arquivo within the for. I added that to the answer

  • Thank you so much for your help!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.