1

Using the following script to access CSV items.

#!/bin/bash
awk -F "|" 'NR > 0 {print $1}' UserAgents.csv

When running the script I am getting the correct output, i.e. the entire set of values in the first 'column' of the CSV are printed to the terminal. What I would like to add is to read these items one by one and perform some operation on them like concatenate it with a string, and then output them (to file, pipe, or terminal) one by one.

2
  • 2
    your {print $1} is getting executed per item, one by one. So why not do the action there, try print "hello there " $1 Not putting as an answer, because i feel there might be something else in the problem Commented Apr 23, 2013 at 15:49
  • 1
    NR > 0 - if that is to ignore blank lines it should be NF > 0 Commented Apr 23, 2013 at 15:58

1 Answer 1

2

This should make it clear what your awk script is doing:

awk -F '|' '{
   print NR, NF, $1, "with some trailing text"
}' UserAgents.csv
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks it helped me understand the way awk processed entries in a CSV file. Although I ended up using IFS for the final version of my script.
What does I ended up using IFS for the final version of my script mean? There is no IFS bultin variable in awk but there is in shell.
Sorry for the confusion, I meant that I ended up not using awk at all.
@RaviGupta then you made the wrong choice as this is 100% a job for awk. It's the kind of thing awk was invented for.
I can share my final script if that will help you help me understand the deficiencies in my solution.
|

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.