Solved

Need proper formatting for outputing text data to Excel

Posted on 2015-02-24
1
59 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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
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…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

772 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