Solved

Need proper formatting for outputing text data to Excel

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
CENTOS DHCP Server / PXE/TFTP 14 202
lunix and unix command 21 111
Web resource - Man pages for SUSE Enterprise Linux 11 1 55
cscript to activate Windows and Office? 2 76
Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
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…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

685 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