Solved

Need proper formatting for outputing text data to Excel

Posted on 2015-02-24
1
55 Views
Last Modified: 2015-03-21
I am currently looping through files in a directory and outputting the file name, followed by 11 fields of relevant information.

Currently when I'm trying to output the data into a CSV file, its all in the same column (I need two columns)

#!/bin/bash

tmp_file=/tmp/test1_file.csv
> $tmp_file

for file in /home/jlefler/Desktop/EPA/*; do
   echo -e "${file##*/}\t\c" >> $tmp_file
   awk 'BEGIN{OFS="     "};{if (NF > 9) print $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11}' $file >> $tmp_file
done

Open in new window


Suggestions would be very helpful.

To summarize, I need the CSV file to have two columns: the first column is the file name and the second column is several tab delimited files.
0
Comment
Question by:TampaJay
1 Comment
 
LVL 13

Accepted Solution

by:
Mark Bullock earned 500 total points
ID: 40629183
Use the -v switch to pass in the environment variable. Then print its value and the comma at the beginning of each line.

Something like this:
awk -v myfile=$file 'BEGIN{OFS="     "};{if (NF > 9) print myfile, ",", $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11}' $file >> $tmp_file

Open in new window

0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now