Petar Nikolov
Petar Nikolov

Reputation: 321

Shell script that filters command output and saves it in Json formated list

never worked with shell scripts before,but i need to in my current task.
So i have to run a command that returns output like this:

    awd54a7w6ds54awd47awd refs/heads/SomeInfo1  
    awdafawe23413f13a3r3r refs/heads/SomeInfo2 
    a8wd5a8w5da78d6asawd7 refs/heads/SomeInfo3 
    g9reh9wrg69egs7ef987e refs/heads/SomeInfo4 

And i need to loop over every line of output get only the "SomeInfo" part and write it to a file in a format like this:

    ["SomeInfo1","SomeInfo2","SomeInfo3"]

I've tried things like this:

    for i in $(some command); do
      echo $i | cut -f2 -d"heads/" >> text.txt
    done

But i don't know how to format it into an array without using a temporary file.
Sorry if the question is dumb and probably too easy and im sure i can figure it out on my own,but i just don't have the time for it because its just an extra conveniance feature that i personally want to implement.

Upvotes: 2

Views: 91

Answers (4)

stack0114106
stack0114106

Reputation: 8711

Using Perl one-liner

$ cat petar.txt
    awd54a7w6ds54awd47awd refs/heads/SomeInfo1
    awdafawe23413f13a3r3r refs/heads/SomeInfo2
    a8wd5a8w5da78d6asawd7 refs/heads/SomeInfo3
    g9reh9wrg69egs7ef987e refs/heads/SomeInfo4

$ perl -ne ' { /.*\/(.*)/ and push(@res,"\"$1\"") } END { print "[".join(",",@res)."]\n" }' petar.txt
["SomeInfo1","SomeInfo2","SomeInfo3","SomeInfo4"]

Upvotes: 0

nsa
nsa

Reputation: 565

You can also use awk without any loops I guess:

cat prev_output | awk -v ORS=',' -F'/' '{print "\042"$3"\042"}' | \
sed 's/^/[/g ; s/,$/]\n/g' > new_output

cat new_output
["SomeInfo1","SomeInfo2","SomeInfo3","SomeInfo4"]

Upvotes: 0

David C. Rankin
David C. Rankin

Reputation: 84551

While you should rarely ever use a script to format json, in your case you are simply parsing output into a comma-separated line with added end-caps of [...]. You can use bash parameter expansion to avoid spawning any additional subshells to obtain the last field of information in each line as follows:

#!/bin/bash

[ -z "$1" -o ! -r "$1" ] && {   ## validate file given as argument
    printf "error: file doesn't exist or not readable.\n" >&2
    exit 1
}

c=0     ## simple flag variable

while read -r line; do                  ## read each line
    if [ "$c" -eq '0' ]; then           ## is flag 0?
        printf "[\"%s\"" "${line##*/}"  ## output ["last"
    else                                ## otherwise
        printf ",\"%s\"" "${line##*/}"  ## output ,"last"
    fi
    c=1         ## set flag 1
done < file     ## redirect file to loop
echo "]"        ## append closing ]

Example Use/Output

Using your given data as the input file, you would get the following:

$ bash script.sh file
["SomeInfo1","SomeInfo2","SomeInfo3","SomeInfo4"]

Look things over and let me know if you have any questions.

Upvotes: 0

Elias Toivanen
Elias Toivanen

Reputation: 848

Try this

# json_encoder.sh
arr=()
while read line; do
  arr+=(\"$(basename "$line")\")
done
printf "[%s]" $(IFS=,; echo "${arr[*]}")

And then invoke

./your_command | json_encoder.sh

PS. I personally do this kind of data massaging with Vim.

Upvotes: 2

Related Questions