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
9

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.