Cut paste from one file to other


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Cut paste from one file to other
# 1  
Old 12-09-2010
Cut paste from one file to other

Hello,
I am working on unix for the first time.
I have to write a shell script where i want to cut paste from one file to other.

File "1234.abc" is
Code:
03,12345555
16,936,x,x,120
16,936,x,x,100
49,12345555
03,12347710
16,936,x,x,115
16,936,x,x,122
49,12347710
03,12342222
16,936,x,x,146
16,926,x,x,88
49,12342222

Here in "03,12345555", 03 indicates line id and 12345555 indicates account number of user. The subsiquent lines are details for account number 12345555 till record "49,12345555"
From next line, details for new account number eg.12347710 starts

My requirement is if i give parameter as 12347710, all records for this accounts should be cut from File "1234.abc" and should be pasted in new file "1234.7710" (ie name of original file and extn is last four digits of parameter)

so new "1234.abc" will be
Code:
03,12345555
16,936,x,x,120
16,936,x,x,100
49,12345555
03,12342222
16,936,x,x,146
16,926,x,x,88
49,12342222

and "1234.7710" will be
Code:
03,12347710
16,936,x,x,115
16,936,x,x,122
49,12347710


Last edited by Franklin52; 12-09-2010 at 08:05 AM.. Reason: Please use code tags, thank you
# 2  
Old 12-09-2010
If your input file is having non variant data, I mean starting account data with id 30 and than 2 lines of data with id 16 and finishes with id 49 then I have a very simple solution for it.

Code:
xargs -d"\|" -n4 < 1234.abc | grep 12347710 | tr '\|' '\n' > 1234.7710

If data is variant then revert..Smilie
This User Gave Thanks to For This Post:
R0H0N
# 3  
Old 12-09-2010
Code:
f=1234.abc
p=12345555
t=${p%????}
ext=${p#$t}
nawk -F"," -v P="$p" -v c="${f%.*}.${ext}" -v n="${f%.*}.new" '(NF==2){f=($2==P)?c:n}{print $0 > f}' "$f"

Code:
# cat 1234.5555
03,12345555
16,936,x,x,120
16,936,x,x,100
49,12345555
# cat 1234.new
03,12347710
16,936,x,x,115
16,936,x,x,122
49,12347710
03,12342222
16,936,x,x,146
16,926,x,x,88
49,12342222


Code:
# p=12347710
# t=${p%????}
# ext=${p#$t}
# nawk -F"," -v P="$p" -v c="${f%.*}.${ext}" -v n="${f%.*}.new" '(NF==2){f=($2==P)?c:n}{print $0 > f}' 1234.abc

Code:
# cat 1234.7710
03,12347710
16,936,x,x,115
16,936,x,x,122
49,12347710
# cat 1234.new
03,12345555
16,936,x,x,120
16,936,x,x,100
49,12345555
03,12342222
16,936,x,x,146
16,926,x,x,88
49,12342222


Last edited by ctsgnb; 12-09-2010 at 10:23 AM..
This User Gave Thanks to ctsgnb For This Post:
# 4  
Old 12-13-2010
ROHON and ctsgnb

I got sample file from funtional guy which look like

Quote:
01,,BNZA,101103,0400,1,78,78/
02,BNZA,084-004,1,101102,0000/
03,047316555,AUD,015,000,100,11164907,102,100,400/
88,11164907,402,500,500,000,501,000,502/
88,000,503,000,965,000,966,400/
88,967,000,968,000,969,000/
16,930,11164907,0,0,FROM: 04-737-7577
16,475,60000,0,0046887/
16,501,6512446,0,0,FD000004793
16,501,3719353,0,0,002150010544
16,501,632224,0,0,002150010742
16,501,240884,0,0,002150010741
49,44660628,44660228/
03,047374624,AUD,015,000,100,000,102,000,400/
88,000,402,000,500,000,501,000,502/
88,000,503,000,965,000,966,400/
88,967,000,968,000,969,000/
49,400,000/
03,047377710,AUD,015,000,100,86840225,102,13100,400/
88,86840225,402,5900,500,000,501,000,502/
88,000,503,000,965,000,966,400/
88,967,000,968,000,969,000/
16,915,91695,0,000000000002/
16,915,228465,0,000000000009/
16,915,6955,0,000000000011/
16,915,106620,0,000000000013/
16,915,41830,0,000000000016/
16,915,224650,0,000000000017/
16,915,112085,0,000000000019/
16,915,25750,0,000000000049/
16,915,8440,0,000000000059/
16,915,81200,0,000000000081/
16,915,114480,0,000000000086/
49,347380300,347379900/
98,1058972775,3,1058972775/
99,1898953341,1,272,1898948248/

whenever i give account number (which will be second field on record id 03), all records for that account number (from line 03 to line 49) should be cut paste to new file
# 5  
Old 12-13-2010
Try
Code:
xargs -d"\|" file | tr '|03,' '\n' | grep "^03,047316555"| tr '\|' '\n'

This User Gave Thanks to For This Post:
R0H0N
# 6  
Old 12-13-2010
For example, i have put the following code in a script call "ak"
Code:
f=tst.abc
p=$1
t=${p%????}
ext=${p#$t}
awk -F"," -v P="$p" -v c="${f%.*}.${ext}" -v n="${f%.*}.new" 'BEGIN{o=n}/^03/{o=($2~P)?c:n}{print $0 > o}' "$f"

I put your sample file in a file called tst.abc
Code:
$ cat tst.abc
01,,BNZA,101103,0400,1,78,78/
02,BNZA,084-004,1,101102,0000/
03,047316555,AUD,015,000,100,11164907,102,100,400/
88,11164907,402,500,500,000,501,000,502/
88,000,503,000,965,000,966,400/
88,967,000,968,000,969,000/
16,930,11164907,0,0,FROM: 04-737-7577
16,475,60000,0,0046887/
16,501,6512446,0,0,FD000004793
16,501,3719353,0,0,002150010544
16,501,632224,0,0,002150010742
16,501,240884,0,0,002150010741
49,44660628,44660228/
03,047374624,AUD,015,000,100,000,102,000,400/
88,000,402,000,500,000,501,000,502/
88,000,503,000,965,000,966,400/
88,967,000,968,000,969,000/
49,400,000/
03,047377710,AUD,015,000,100,86840225,102,13100,400/
88,86840225,402,5900,500,000,501,000,502/
88,000,503,000,965,000,966,400/
88,967,000,968,000,969,000/
16,915,91695,0,000000000002/
16,915,228465,0,000000000009/
16,915,6955,0,000000000011/
16,915,106620,0,000000000013/
16,915,41830,0,000000000016/
16,915,224650,0,000000000017/
16,915,112085,0,000000000019/
16,915,25750,0,000000000049/
16,915,8440,0,000000000059/
16,915,81200,0,000000000081/
16,915,114480,0,000000000086/
49,347380300,347379900/
98,1058972775,3,1058972775/
99,1898953341,1,272,1898948248/

I run the script passing as argument an account number :
Code:
$ ksh ak 047316555

Then check the content of the generated files :
Code:
$ cat tst.6555
03,047316555,AUD,015,000,100,11164907,102,100,400/
88,11164907,402,500,500,000,501,000,502/
88,000,503,000,965,000,966,400/
88,967,000,968,000,969,000/
16,930,11164907,0,0,FROM: 04-737-7577
16,475,60000,0,0046887/
16,501,6512446,0,0,FD000004793
16,501,3719353,0,0,002150010544
16,501,632224,0,0,002150010742
16,501,240884,0,0,002150010741
49,44660628,44660228/

Code:
$ cat tst.new
01,,BNZA,101103,0400,1,78,78/
02,BNZA,084-004,1,101102,0000/
03,047374624,AUD,015,000,100,000,102,000,400/
88,000,402,000,500,000,501,000,502/
88,000,503,000,965,000,966,400/
88,967,000,968,000,969,000/
49,400,000/
03,047377710,AUD,015,000,100,86840225,102,13100,400/
88,86840225,402,5900,500,000,501,000,502/
88,000,503,000,965,000,966,400/
88,967,000,968,000,969,000/
16,915,91695,0,000000000002/
16,915,228465,0,000000000009/
16,915,6955,0,000000000011/
16,915,106620,0,000000000013/
16,915,41830,0,000000000016/
16,915,224650,0,000000000017/
16,915,112085,0,000000000019/
16,915,25750,0,000000000049/
16,915,8440,0,000000000059/
16,915,81200,0,000000000081/
16,915,114480,0,000000000086/
49,347380300,347379900/
98,1058972775,3,1058972775/
99,1898953341,1,272,1898948248/
$

If you want to generate a file for each and every account you can for example go for the following :

Code:
awk -F"," 'BEGIN{o="dummy"}/^03/{o="account"$2}{print $0 > o ; if(/^49/) o="dummy"}' tst.abc


Last edited by ctsgnb; 12-13-2010 at 05:55 PM..
This User Gave Thanks to ctsgnb For This Post:
# 7  
Old 12-14-2010
Code:
 var=047377710;sed -n "/^03,$var/,/^49,/p" infile > newfile

This User Gave Thanks to Scrutinizer For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to cut a pipe delimited file and paste it with another file to form a comma separated outputfile

Hello ppl I have a requirement to split (cut in unix) a file (A.txt) which is a pipe delimited file into A1.txt and A2.txt Now I have to join (paste in unix) this A2.txt with external file A3.txt to form output file A4.txt which should be CSV (comma separated file) so that third party can... (25 Replies)
Discussion started by: etldev
25 Replies

2. Shell Programming and Scripting

Problem in cut or paste

Hello, I have some problem in cut or paste command for my text data. Input1.txt : I use cut command : cut -d ' ' -f1 Input1.txt > result.txt result.txt : Then, I use paste command to merge result.txt. paste -d ' ' result.txt Input1.txt > output.txt output.txt showed : I use cut... (1 Reply)
Discussion started by: awil
1 Replies

3. Shell Programming and Scripting

Cut, replace and Paste a String from one file to another

I need to cut all the Strings in one file and Paste it in the another file in the Specific line by replacing the specific String. For Example Step 1: From the newfile.txt, i need to copy all the strings newfile.txt How are you, I am fine, How is your work newfle2.txt Hello david,... (2 Replies)
Discussion started by: Padmanabhan
2 Replies

4. Shell Programming and Scripting

Cut and paste data in new file

HI Guys, I have file A: Abc XyZ Abc Xyz Kal Kaloo Abc XyZ Abc Xyz Kalpooo Abc XyZ Abc Xyz Kloo Abc Abc Klooo I want file B Abc XyZ Abc Xyz Kal Kaloo Abc XyZ Abc Xyz Kalpooo Abc XyZ Abc Xyz Kloo File A is now 1 lines Abc Abc Klooo Cut all lines which have xyz... (2 Replies)
Discussion started by: asavaliya
2 Replies

5. Shell Programming and Scripting

Help required the cut the whole contents from one file and paste it into new file

Hi, First of all sincere apologies if I have posted in a wrong section ! Please correct me if I am wrong ! I am very new to UNIX scripting. Currently my problem is that I have a code file at the location /home/usr/workarea/GeneratedLogs.log :- Code :- (Feb 7, 571 7:07:29 AM),... (4 Replies)
Discussion started by: acidburn_007
4 Replies

6. Shell Programming and Scripting

need help with cut and paste command

I have a file which contains 3 fields separated by tabs example andrew kid baker I need to swap kid and baker using cut and paste commands how is this to be done? Thanks (3 Replies)
Discussion started by: drew211
3 Replies

7. UNIX for Dummies Questions & Answers

Need help with using cut and paste command

I have a file which contains 3 fields separated by tabs example andrew kid baker I need to swap kid and baker using cut and paste commands how is this to be done? Thanks (1 Reply)
Discussion started by: drew211
1 Replies

8. Shell Programming and Scripting

cut and paste

Hi, Need a help with shell script. I have to search for a string in one of the file, if match found, copy the line to a new file and delete the line from the exisiting file. eg: 83510000000000063800000.1800000.1600000.1600000.2400000.1800000.2000000.21... (6 Replies)
Discussion started by: gpaulose
6 Replies

9. Shell Programming and Scripting

cut and paste?

hi, I have a file with content like this for an employee: EmployeeID 101 Day_type, day vacation,1/2/2009 sick day, 3/2/2009 personal day, 4/5/2009 jury duty day, 5/5/2009 how do I make the result to show: EmployeeID,Day_type,day 101,vacation,1/2/2009 101,sick day,... (6 Replies)
Discussion started by: jbchen
6 Replies

10. Shell Programming and Scripting

cut & paste

hi i am new to shell scripting, i have been trying to cut columns numbered 1,4 of a file consisiting of 4 columns. Each column is seperated by 2 spaces. for example: john 6102097199 tennessee usa michel 6734590899 texas USA now, i need to cut the name... (3 Replies)
Discussion started by: t_harsha18
3 Replies
Login or Register to Ask a Question