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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.