0

I am learning about "AWK". Well, I need to output awk command on variable to parse it. The file have 130000 lanes. I need put with AWK a column like array to use the variable in other part of the script. Sorry for my english and ask me if you dont understand my objetive.

well the code:

awk '/file:/{ name=$3 ; print name ;)' ejemplo.txt

I try:

list=$(awk '/file:/{ name=$3 ; print name;)' ejemplo.txt)

but when I try to show the content in the variable $list, only show me 1 lane. I tried declare array but only show me 1 result

anyone understand what happen? how can i build a array with all the output?

I try this code to build a array with AWK. Maybe I am a little dumb but i dont look how to solve my problem:

#!/bin/bash
#filename: script2.sh

conta=$(cat ejemplo_hdfs.txt | wc -l)

for i in `seq 0 $conta`;do
        objeto=" "
        owner=" "
        group=" "
        awk '{
                if($1=="#" && $2=="file:") objeto=$3;
                else if($1=="#" && $2=="owner:") owner=$3;
                else if($1=="#" && $2=="group:") group=$3;
                else
                        print $3
                ;}' ejemplo_hdfs.txt
        echo $objeto+","+$owner+","+$group

done
LCabaAres
  • 15
  • 1
  • 10
  • 2
    Try `list=($(awk ...))` – Shawn Sep 13 '19 at 10:27
  • `list="$(awk '/file:/{ name=$3 ; print name;)' ejemplo.txt)"`? – Wiktor Stribiżew Sep 13 '19 at 10:27
  • 4
    Don't store result of `awk` in a shell array. Do remaining processing in awk itself. – anubhava Sep 13 '19 at 10:30
  • dont work anyone of the syntax. So to build a var with the result of awk i cant output the var from awk command. – LCabaAres Sep 13 '19 at 10:51
  • So how can solve this problem? Maybe declare the var in awk and concat with other var? – LCabaAres Sep 13 '19 at 12:59
  • You wrote *"The file have 130000 lanes"*. Lanes? Do you mean rows, or columns or charaters per line? ;-) In any case, without a better description of your real problem, best to do all your processing in `awk`.. @Shawn 's idea above is good, but you also need to know the notation for accessing arrays like that. `echo "${array[@]}"` give you the whole array, and `echo "${array[1]}"` gives you the 2nd element of the array. Good luck. – shellter Sep 13 '19 at 14:28
  • When you say `lane`, I think you mean `line`, right? – Ed Morton Sep 13 '19 at 17:35
  • 2
    Possible duplicate of [Bash capturing output of awk into array](https://stackoverflow.com/q/15105135/608639), [Put result of awk into an array](https://stackoverflow.com/q/21130417/608639), [Print array elements on separate lines in Bash?](https://stackoverflow.com/q/15691942/608639), [How to print a bash array on the same line](https://stackoverflow.com/q/39985683/608639), [How to echo all values from array in bash](https://stackoverflow.com/q/41150814/608639), etc. – jww Sep 14 '19 at 03:28
  • yes, its line. Sorry for my english. It doesnt post repeat. I need to pick one work of every line after check with regex. I need build a line with 5 words of diferents lines. – LCabaAres Sep 14 '19 at 23:24

1 Answers1

1

To assign an array to a variable in bash, the whole expression that generates the elements needs to be in parenthesis. Each word produced by the evaluated expression becomes an element of the resulting array.

Example:

#!/bin/bash
foo=($(awk -F, '{print $2}' x.txt))
# Size of array
echo "There are ${#foo[@]} elements"
# Iterate over each element
for f in "${foo[@]}"; do
    echo "$f"
done
# Use a specific element.
echo "The second element is ${foo[1]}"
$ cat x.txt
1,a dog
2,b
3,c
$ ./array_example.sh
There are 4 elements
a
dog
b
c
The second element is dog
Shawn
  • 47,241
  • 3
  • 26
  • 60