Unix command doubt

Hi Experts...
Can anyone explain this unix command bit-by-bit.
Please help...
Unix Command:
sed '/^$/d' $csv_file | egrep -v '(^,|^ )' | nawk -F "," '{OFS="|";print $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15}' | nawk -F "|" '{i=1; while(i<=NF) {if ($i=="") {printf (" |")} else {printf ("%s|",$i)}; i++}; printf("\n")}' | sed 's/|$//'

Open in new window

Who is Participating?
sed deletes all empty lines from the $csv file
egrep -v excludes any line that starts with a comma or a space i.e. possibly empty or space in the first column
nawk  the first removes the commas and outputs the items as a list separated by '|' i.e. the commas in the print list are replaced with | as spacers.
The next awk reparses the lines based on the spacer '|'
and print one at a time ... (not completely ....)
the last sed strips the last |.

take a snippet of the csv.
add each command at a time and see what the changes are in the output.
The "nawk" command in the middle prints out each field, replacing empty fields (i.e. zero-length ones) with a single space.

A shorter version, assuming that all of the lines you want are 15 fields long, might be
grep '^[^, ]' $csv_file | sed -e 's/,,/, ,/g' -e 's/,$/, /' | tr , '|'

Open in new window

Kamaraj SubramanianApplication Support AnalystCommented:
Can you provide the sample data of your csv file.

gaugetaAuthor Commented:
@ALL:Thank you for your replies.

@itkamaraj:The sample file is attached.
The file is not in a CSV format, it needs to be converted to a csv for the script to work.
save the document as CSV.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.