2

Im trying to curl a csv file and parse it based on their property and print it back by using variable names.

File 1:

10.0.0.1,gateway,name_of_device
10.2.4.5,server,name_of_device
10.3.5.6,PC,name_of_device

My script below,

#!/bin/sh

input=$(curl http://example.com/1.txt)

ip=$(echo "$input" | awk -F ',' '{print $1}')
type=$(echo "$input" | awk -F ',' '{print $2}')
name=$(echo "$input" | awk -F ',' '{print $3}')

echo "$ip $type $name" 

This prints,

10.0.0.1
10.2.4.5
10.3.5.6
gateway
server
PC
name_of_device
name_of_device
name_of_device

but the expected output should be like,

10.0.0.1 gateway name_of_device
10.2.4.5 server name_of_device
10.3.5.6 PC name_of_device

Tried different options like,

assigning output to another variable and echo'ing it,

# output="$source_ip $tag_text"
# echo -e $output

printf statements:

# printf "%-20s | %-20s" "$source_ip" "$tag_text"


# printf "$source_ip" "$tag_text"

2 Answers 2

2

awk is certainly the correct tool to use for this, but you can also do:

curl http://example.com/1.txt |
while IFS=, read ip type name rest; do
  echo $ip $type $name
done

The variables will lose their value after the while loop finishes, since it is in a sub-shell.

Sign up to request clarification or add additional context in comments.

1 Comment

... and if you want to have the variables in the main shell, use process substitution with while ... done < <(curl ...)
2

Just set the input field separator to the comma and let awk handle the rest of it:

$ awk -F, '$1=$1' file
10.0.0.1 gateway name_of_device
10.2.4.5 server name_of_device
10.3.5.6 PC name_of_device

By saying $1=$1, awk recalculates the fields and sets OFS all around, which makes all commas (FS) to be replaced with spaces (OFS).

10 Comments

You didn't $1=$1. That causes awk to rebuild the record and actually utilize the OFS.
yes, this works. But how can i parse as rows? if I call an IP (first column), it should provide appropriate type and name (which is 2nd and 3rd column).
@Karthik How do you "call an IP" ? Maybe using grep to retrieve the line with the specified IP is what you want?
@Aaron may be yes.
@fedorqui yes, understood. My final goal is to push these data into some db for other purposes. I really haven't started looking into db importing and it was too early to post that question here. So the db would contain a defied set of columns and that why i have to ignore them inside this script itself. Your suggestions was very helpful. thanks
|

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.