How to store it in a file and help with code??


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to store it in a file and help with code??
# 1  
Old 02-03-2011
How to store it in a file and help with code??

I have two files "SAL.lis" and "accts.fmt" i have to select data from few fields and store them in a seperate file as i explained below..script please????

SAL.lis
Code:
08231|53031-SC98|2497|105|0000|0000|000|CYACT|Pd_1|2728.48

accts.fmt
Code:
53031|Revenue - Cigarette Stamping|53000|||||

Logic

Take 5 digit (53031) Account number (Second field) of the SAL.lis
file and look for that account number in the accts.fmt file
If match is found take the Second Field in the SAL.lis file
and the account Description (second field) from the accts.fmt file.

Output:
Code:
53031-SC98|Revenue - Cigarette Stamping


The above output should be stored in a seperate file "SAL_EII_Desc"

I tried it out with cut but not getting.

Thanks in advance

Last edited by Franklin52; 02-03-2011 at 06:49 AM.. Reason: Please use code tags, thank you
# 2  
Old 02-03-2011
One way:
Code:
AWK -F\| 'NR==FNR{n=int($2); a[n]=$2; next}
a[$1]{print a[$1] FS $2}
' SAL.lis accts.fmt

# 3  
Old 02-03-2011
awk: syntax error near line1
awk: bailing out near line 1

This is not working.. and also please let me know how to store this output in a seperate file.
SAL_EII_Desc

---------- Post updated at 05:05 PM ---------- Previous update was at 04:59 PM ----------

Am using korn shell...

what i tried out :

Code:
#!/bin/ksh
b="cut -f2 -d "|" SAL.lis | cut -f1 -d "_" ";
echo $b

---------- Post updated at 05:12 PM ---------- Previous update was at 05:05 PM ----------

both the files "SAL.lis" and "accts.fmt" have some thousands of records like that.
# 4  
Old 02-03-2011
Quote:
Originally Posted by Diddy
awk: syntax error near line1
awk: bailing out near line 1

This is not working..
Use nawk or /usr/xpg4/bin/awk on Solaris.

Quote:
and also please let me know how to store this output in a seperate file.
SAL_EII_Desc
Redirect the output to the file:
Code:
awk 'commands...' SAL.lis accts.fmt > SAL_EII_Desc

# 5  
Old 02-03-2011
Its Working...Thanks..and Sorry for bothering you.. I am still in learning stage, don't know how to use those commands. Thanks for your help. Can you please explain that if you have time!
Code:
AWK -F\| 'NR==FNR{n=int($2); a[n]=$2; next}
a[$1] Here am trying to understand it like this.
NR==FNR // file1==file2 

{

n=int($2);//n=53031-SC98 ;

a[n]=$2;//a[0]=53031-SC98 , a[1]=.....

next //??? here i dont understand

}

a[$1] // picking a[0] i.e 53031-SC98

{

print a[$1] FS $2 // printing .. O/P:: 53031-SC98 |Revenue - Cigarette Stamping

}

Here i am not able to understand how and where they are compared and printed???? please help me out


Thanks

---------- Post updated at 09:20 PM ---------- Previous update was at 07:23 PM ----------

Some one explain this please...Thanks
# 6  
Old 02-03-2011
Code:
awk -F\| 'NR==FNR{n=int($2); a[n]=$2; next}
a[$1]{print a[$1] FS $2}
' SAL.lis accts.fmt

Explanation:

NR==FNR if we read the first file
n=int($2) n is the part before the "-" (53031)
a[n]=$2 assign the second field to the element of array a with n as index
next read the next line of the first file
a[$1] if the first field of the second file is an index in array a
print a[$1] FS $2 print the 2nd field of the 1st file and the second field of the 1st file

Have a read of this if something is incomprehensible:

Awk - A Tutorial and Introduction - by Bruce Barnett
# 7  
Old 02-07-2011
Hi Franklin..Need small help. Trying out similar stuff.

For the ouput file i need to mention some column names like this, So where i can mention in the script to get output like this

Code:
nawk -F\| 'NR==FNR{n=int($2); a[n]=$2; next}a[$1]{print a[$1] FS $2}' SC.lis ext.fmt > outfile


outfile:

Quote:
Accounts-Product|Service Department

53031-SC98|Revenue - Cigarette Stamping
53032-SC99|Revenue - Malls
Moderator's Comments:
Mod Comment Note to Diddy: Use forum code tags and also watch your spelling errors.
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Hit multiple URL from a text file and store result in other test file

Hi, I have a problem where i have to hit multiple URL that are stored in a text file (input.txt) and save their output in different text file (output.txt) somewhat like : cat input.txt http://192.168.21.20:8080/PPUPS/international?NUmber=917875446856... (3 Replies)
Discussion started by: mukulverma2408
3 Replies

2. Shell Programming and Scripting

Store and isolate bad pages from a file to new file

I have a file like below . The good pages must have 3 conditions : The pages that containing page total only must have 50 lines. The pages that containing customer total only must have 53 lines. The last page of Customer Total should be the last page. How can I accomplish separating good... (1 Reply)
Discussion started by: ehabaziz2001
1 Replies

3. UNIX for Dummies Questions & Answers

Writing a script that will take the first line from each file and store it in an output file

Hi, I have 1000 files names data1.txt through data1000.txt inside a folder. I want to write a script that will take each first line from the files and write them as output into a new file. How do I go about doing that? Thanks! (2 Replies)
Discussion started by: evelibertine
2 Replies

4. UNIX for Dummies Questions & Answers

How to grep and store in a file

Guys, I was wondering what command can be used to parse the "LaxOrdID" field into a separate file? These messages are in thousands and I need to perform a comparision. (6 Replies)
Discussion started by: DallasT
6 Replies

5. Shell Programming and Scripting

To read and separate number and words in file and store to two new file using shell

hi, I am a begginer in unix and i want to know how to open a file and read it and separate the numbers & words and storing it in separate files, Using shell scripting. Please help me out for this. Regards S.Kamakshi (2 Replies)
Discussion started by: kamakshi s
2 Replies

6. Shell Programming and Scripting

How to store Data in a File

I want to read a data from a read command and store it in a file...... Plz send me how I can do that Ex: read val and I want to store this val in a file called temp. (2 Replies)
Discussion started by: krishna_sicsr
2 Replies

7. UNIX for Dummies Questions & Answers

Need to create .bat file to store log file

Hi guys. Can someone point me to a resource that explains this? Basically these are websphere logs that need to be stored daily, I'm on sunOS 5.8. Each new file stored could have the current time as its filename. The script could be run on a cron which I can set up. I'm just not sure how to write... (0 Replies)
Discussion started by: dirtybrown
0 Replies

8. Shell Programming and Scripting

Store return code of shell script in oracle table

Hi friends, I have to do the following things : 1) there should be a shell script returning the returning the return code of the script. and i have to add some more details like on which machine is has run , at what time and other details and then using plsql i have to add a row to Oracle... (3 Replies)
Discussion started by: sveera
3 Replies
Login or Register to Ask a Question