Need script to generate file.


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Need script to generate file.
# 1  
Old 03-13-2014
Need script to generate file.

Hi
I have a file "test" with data as below

Code:
1,APRIL,NEW,"New market,delhi,pune,India",RECENT, 254664
2,MARCH,OLD,"New Area,Mumbai,UP,India",CURRENT, 152483

So I want a script which provides output as below
Code:
1,APRIL,RECENT,254664
2,MARCH,CURRENT,152483

I am aware we can use awk/sed to generate below output but not sure how to handle with data inside quotes "".
Please help with script as requested. Many thanks in Advance
# 2  
Old 03-13-2014
Code:
awk -F, '{ print $1,$2,$(NF-1),$NF}' OFS=, file

These 2 Users Gave Thanks to Yoda For This Post:
# 3  
Old 03-13-2014
Quote:
Originally Posted by sv0081493
I am aware we can use awk/sed to generate below output but not sure how to handle with data inside quotes "".
The idea is simply to define what a "field" is, by defining what separates one such field from the other. There is a variable "IFS" (Internal Field Separator), which does exactly this for Unix lines. Per default it is set to blanks which is why blanks will separate a unix command from its options on the command line. In your case, this separator is "," (comma) and no double quote will interfere once you set the IFS to ",".

The shell command "read var1 [var2 var3 ...]" will read a line, then put the first field to var1, the second to var2, and so on. If there are more fields than variables, the rest of the line goes to the last variable (therefore, if you specify only one everything goes to it), it there are more variables than fields in a line the last variables are empty. You can try that with the following code (i suggest you play around with it a few times to understand the effect:

Code:
echo "a,b,c,d,e,f,g,h" | IFS="," read var1 var2 var3 var4
echo var1=$var1
echo var2=$var2
echo var3=$var3
echo var4=$var4

You should now be ready to understand the following (and eventually adapt it to your needs):

Code:
while IFS="," read f1 f2 junk junk f3 f4 ; do
     echo "$f1,$f2,$f3,$f4"
done < /path/to/input/file

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 4  
Old 03-13-2014
It's not that simple if the , within the quotes should not be field delimiters.
Especially if their count varies.
Yoda's solution works around it.

---------- Post updated at 03:32 PM ---------- Previous update was at 03:28 PM ----------

bakunin, your first example only works with ksh; most shells require a block
Code:
echo "a,b,c,d,e,f,g,h" | {
IFS="," read var1 var2 var3 var4
echo var1=$var1
echo var2=$var2
echo var3=$var3
echo var4=$var4
}

or a here document
Code:
IFS="," read var1 var2 var3 var4 << "_EOT"
a,b,c,d,e,f,g,h
_EOT
echo var1=$var1
echo var2=$var2
echo var3=$var3
echo var4=$var4


Last edited by MadeInGermany; 03-13-2014 at 05:52 PM..
This User Gave Thanks to MadeInGermany For This Post:
# 5  
Old 03-13-2014
Hi.

My goal in this was to allow the most straight-forward awk statement { print $1,$2,$5,$6 } to solve the problem Of course, as MadeInGermany observed, there are quoted strings in which commas are embedded.

So one thing we could do is to reformat the csv file so that the field separators are something other than commas. This can be done relatively easily with extant perl modules, and we just need to learn how to use them.

So here is a shell script that allows, at the heart, the simple awk print statement noted above:
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate separator replacement with perl.

echo
what ~/bin/divepm
# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C divepm perl

pl " Modules and versions used by demo perl script p1:"
divepm -q -i=p1

FILE=${1-data1}

pl " Input data file $FILE:"
cat $FILE

pl " Expected output:"
cat expected_output.txt

pl " Results:"
./p1 ',|' $FILE |
tee f1 |
awk 'BEGIN { FS="|"; OFS="|" }{ print $1,$2,$5,$6 }' |
tee f2 |
./p1 '|,'

pl " Demo perl script:"
cat p1

exit 0

producing:
Code:
$ ./s1

divepm	Display version of perl modules.

Environment: LC_ALL = , LANG = en_US.UTF-8
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian 5.0.8 (lenny, workstation) 
bash GNU bash 3.2.39
divepm (local) 1.4
perl 5.10.0

-----
 Modules and versions used by demo perl script p1:
 1.06	Text::CSV
 1.08	Carp

-----
 Input data file data1:
1,APRIL,NEW,"New market,delhi,pune,India",RECENT, 254664
2,MARCH,OLD,"New Area,Mumbai,UP,India",CURRENT, 152483

-----
 Expected output:
1,APRIL,RECENT,254664
2,MARCH,CURRENT,152483

-----
 Results:
1,APRIL,RECENT,254664
2,MARCH,CURRENT,152483

-----
 Demo perl script:
#!/usr/bin/env perl

# @(#) p1	Demonstrate reconfigure separator for CSV file.
# $Id: p1,v 1.2 2014/03/13 21:47:41 drl Exp drl $

# No checking for errors in this demo version.
# Input separators from command-line,
# Initialize the CSV input and output objects,
# Loop until EOF:
# Read line,
# Parse line with first separator,
# Combine fields into line,
# Print line with new separator.

use Text::CSV;
use Carp;

$comma = ",";
$bell  = "";
$bell  = "|";

$t1   = shift;
$inps = substr( $t1, 0, 1 );
$outs = substr( $t1, 1, 1 );

$csv = Text::CSV->new( { sep_char => $inps, allow_whitespace => 1 } );
$out = Text::CSV->new( { sep_char => $outs } );

while (<>) {
  chomp;
  $line   = $_;
  $status = $csv->parse($line);
  if ( not $status ) {
    carp("problem with csv-parse at $.");
    print "Line $. -- $line";
  }
  @columns = $csv->fields();

  $status = $out->combine(@columns);
  if ( not $status ) {
    carp("problem with csv-combine at $.");
    print "Line $. -- $line";
  }
  $line = $out->string();
  print "$line\n";
}

Obviously there is little in the way of error checking, but the main body of code works. The intermediate files f1, f2 can be examined to see the results of the transformations.

Best wishes ... cheers, drl
This User Gave Thanks to drl For This Post:
# 6  
Old 03-14-2014
Quote:
Originally Posted by MadeInGermany
It's not that simple if the , within the quotes should not be field delimiters.
Especially if their count varies.
Yoda's solution works around it.
Oops! This is true. Somehow the commata inside the double quotes totally escaped my attention. Sorry, this was definitely not the best effort of mine.

In this case I'd like to modify my solution the following way to avoid the problem. The sed-script "masks out" every quoted string from the input. This will work as long as you do not need the content of a quoted string to be in output:

Code:
sed 's/"[^"]*"//'g /path/to/input |\
while IFS="," read f1 f2 junk junk f3 f4 ; do
     echo "$f1,$f2,$f3,$f4"
done

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 7  
Old 03-14-2014
Thanks for your prompt help. Much Appreciated

It worked with below script.
Code:
 
awk -F, '{ print $1,$2,$(NF-1),$NF}' OFS=, file

but I have a concern. I dont think this will work if data inside the quotes changed
and word count increased in some rows. What if my data goes like this

Code:
 
1,APRIL,NEW,"New market,delhi,pune,India",RECENT, 2546642
2,MARCH,OLD,"New Area,street cafe,Mumbai,UP,India",CURRENT, 152483

 
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to generate .csv file

Dears,I need your help in this, I have to create a report based on the output file generated by another program. I want to write a shell script for this. The output file generated every 15 minutes but i can’t open it until the end of day so the script will get the file as an input the file will be... (8 Replies)
Discussion started by: abdul2020
8 Replies

2. Shell Programming and Scripting

Script to generate csv file

Dears, I am new in shell world and I need your help in this, I have to create a report based on the output file generated by another program. I want to write a shell script for this. The output file generated every 15 minutes but i can’t open it until the end of day so the script will get the... (3 Replies)
Discussion started by: abdul2020
3 Replies

3. Shell Programming and Scripting

Needed script to FTP a File and generate a quality checksum file

hi all i want a script to FTP a file and should generate a quality checksum file means when I FTP a file from one server to another server it should generate a QC file which should contain timestamp,no.of records in that file Thanks in advance saikumar (3 Replies)
Discussion started by: hemanthsaikumar
3 Replies

4. Shell Programming and Scripting

Script to generate csv file

Hello; I need to generate a csv file that contains a list of all the files in a particular server (from the root directory ie: \) that have a permission stamp of 777. I would like to create the csv so that it contains the following: server name, file name, full path name where file exists,... (17 Replies)
Discussion started by: gvolpini
17 Replies

5. Shell Programming and Scripting

KSH - help needed for creating a script to generate xml file from text file

Dear Members, I have a table in Oracle DB and one of its column name is INFO which has data in text format which we need to fetch in a script and create an xml file of a new table from the input. The contents of a single cell of INFO column is like: Area:app - aam Clean Up Criteria:... (0 Replies)
Discussion started by: Yoodit
0 Replies

6. Shell Programming and Scripting

how to generate html file using script?

Hi Friends I have an requirement that i need to generate html file using script. and the script output shold keep adding to that html file like tablewise. can anyone please help me out in this. thanks Krish. (2 Replies)
Discussion started by: kittusri9
2 Replies

7. Shell Programming and Scripting

To generate the FTP Script file

Hi, I am new to the shell programming., My requirement is , I have an shell file, which call internally the sql file, which generates 4 files on the directory., and then shell has to create the file which contains all the ftp commands to extract the files to different server for later... (1 Reply)
Discussion started by: konankir
1 Replies

8. Shell Programming and Scripting

Modify script to generate a log file

I've seen several examples of scripts in thise forum about having a script generate a log file. I have a script that is run from cron and that monitors a file system for a specfic filename(s) and then performs some actions on them. Normally I call this script from another script (which the one... (2 Replies)
Discussion started by: heprox
2 Replies
Login or Register to Ask a Question