Hi,
I have a fixed width text file without any header row. One of the columns contains a date in YYYYMMDD format.
If the original file contains 3 dates, I want my shell script to split the file into 3 small files with data for each date.
I am a newbie and need help doing this.
panyam
December 21, 2010, 9:46am
2
please post sample input and expected output.
Regards
Ravi
1 Like
Ok... here is a sample.
The first field is Date in YYYYMMDD.
As you can see, there are 3 dates viz. 20100313, 20100306 and 20100227.
The length of the Date field is 8 bytes.
I want my shell script to read this file and create 3 files, each with data for each of the 3 dates.
Thanks in advance.
20100313 25 2899780446536301 0 1 9.99
20100313 22 9819780446541930 0 1 6.99
20100313 15 1539780446194068 0 1 7.99
20100313 25 459780316044936 0 1 11.24
20100313 20 1279780446570978 0 1 11.24
20100306 26 9959780446570961 0 1 5.99
20100306 24 659780316036221 0 1 16.79
20100227 16 189780446539746 0 2 14.98
20100227 25 279780446570978 0 2 20.98
20100227 19 149780446561334 0 2 22.48
20100227 8 499780446194068 0 2 11.98
20100227 22 8379780446539746 0 3 22.47
panyam
December 21, 2010, 10:00am
4
awk '{ print > $1".txt" }' input_file.txt
1 Like
Shorter ;):
awk '{print > $1 ".txt"}' file
@panyam was faster
1 Like
joeyg
December 21, 2010, 10:14am
6
cut -c1-8 <s1.txt >s2.txt
pulling the first 8 columns of file s1.txt into s2.txt
panyam
December 21, 2010, 10:27am
7
I don't think this will serve the purpose !!!
Since OP want the output to be stored on different files based on column1 which is date.
joeyg
December 21, 2010, 11:10am
8
My bad. I misunderstood what was desired for output. I read his request as simply to pull off the first 8 characters.
Cool!
awk worked.
Couple of questions more.
What do I do if I do not want to hardcode the input_file_name in my shell script and pass it as a parameter from the prompt instead (while running the shell script)?
And how do I name the split output files as POS_Volume_<file_date_here>.txt?
awk '{ print > "POS_Volume_"$1".txt" }' $input_file_name
1 Like
Unfortunately, this one is not working.
Just keeps running without returning the prompt.
The previous awk worked like a charm.
How do I fix this one?
Scott
December 28, 2010, 10:16am
12
Change:
$input_file_name
to the name of your input file (without the $)
1 Like
But what if I don't want to hardcode the input filename? I want to pass the input filename dynamically while running the script.
If input_file_name is set correctly before running ,awk, script should run.
Please post the script you are trying to run, showing how filename variable is set along with the awk command and how script is ran.
Scott
December 28, 2010, 2:15pm
15
Hi.
Then, taking the awk from anurag.singh:
[ $# -eq 0 ] && echo "Usage: $0 <filename>" && exit 1
input_file_name=$1
awk '{ print > "POS_Volume_"$1".txt" }' "$input_file_name"