Hi All,
I am new to this forumn as well to the UNIX, I have basic knowledge of UNIX which I studied some years ago, now I have to do some shell scripting to load data into Oracle database using sqlldr utility, whcih I am able to do. I have a requirement where I need to do following operation.
I have one text file in which the data will be in following format for example
.................... FILE1.txt ....................
A1|1234561|010|065|
aaaaa
sssss
ddddd
fffff
A2|1234562|011|066|
qqqq
ww
eeeeeeee
r
A3|1234563|012|067|
ttttttttt
A4|1234564|013|068|
yyyyy
uuu
A5|1234565|014|069|
sdfsdfsd
werw345
feewwe
A6|1234566|015|060|
A7|1234567|016|061|
....................................................
Now from above file I have to split it into 2 different files for example
File_A.txt
....................................................
A1|1234561|010|065|
A2|1234562|011|066|
A3|1234563|012|067|
A4|1234564|013|068|
A5|1234565|014|069|
A6|1234566|015|060|
A7|1234567|016|061|
....................................................
File_B.txt
....................................................
aaaaa
sssss
ddddd
fffff
qqqq
ww
eeeeeeee
r
ttttttttt
yyyyy
uuu
sdfsdfsd
werw345
feewwe
....................................................
I am able to create the first file FILE_A.txt using awk command, but I am not able create the second FILE_B.txt
I just like to know is there any other quick way of creating 2 seperate files as mentioned above or I can use same awk command to create the second file FILA_B.txt
Any help is highly appreciated.
Regards
JC
Thanks Danmero,
That was really quick and apt solution, I used nawk and it worked out. But I would like to understand how it works, probably I will look into details of nawk man pages
I want to create the 2 files based on above nawk command. I will be dynamically passing the value of project number through runtime input variable
i.e by using read v_projname
if my variable is having v_projname=PROJECT1234
so I have to use it something like $v_projectname in my given nawk command, I tried in various combination but I was not able to get the substituted value of variable
I used in this way nawk '{print > ("ABS\_"((/\|/)?"ADDR\_":"SLICE\_")"$v_projname\_NETWORKID.txt")}' spfile.txt
My output of files shoule be something like
ABS_ADDR_PROJECT1234_NETWORKID.txt
ABS_SLICE_PROJECT1234_NETWORKID.txt
But I am getting
ABS_ADDR_$v_projname_NETWORKID.txt
ABS_SLICE_$v_projname_NETWORKID.txt
Similar to invar1 variable in nawk I also need one more variable like invar2 to be passed into nawk. This I want to use in the place of NETWORKID
i.e. somthing similar to
nawk -v invar1="$aa" invar2="$bb" '{print > ("ABS\_"((/\|/)?"A\_":"B\_")invar1"\_"invar2".txt")}' spfile.txt
Above syntax is obviously wrong, I just like to know is there a way I can pass two different variables in nawk as above or other alternate way
- - - -
Also can I use Substring in nawk, because I need to find the '|' character in certain position in every line
i.e. I need to pick only those lines whose fifth character is '|' and then put that line in first file or else in second file
Eg:-
if
aa=xxxx
bb=yyyyy
and spfile.txt is