Change entries in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Change entries in a file
# 1  
Old 07-27-2015
Change entries in a file

Hi

My current file looks like this:


Code:
id437  duff    main    57936   79026   1000    +       .       ID=Abf022711;source_id=DUFF380.73.2;
id437  Cuff    stem    57936   79026   1000    +       .       ID=Abf022711.2;source_id=DUFF380.73.2;
id437  duff    main    57936   79026   1000    +       .       ID=Abf022721;source_id=DUFF380.73.2;
id437  Cuff    stem    57936   79026   1000    +       .       ID=Abf022721.2;source_id=DUFF380.73.2;


Is it possible for me to add a parent value for every entry having 3rd tab as stem. So whenever my 3rd tab value is stem, i want to add parent = id value. (no float)


Code:
id437  duff    main    57936   79026   1000    +       .       ID=Abf022711;source_id=DUFF380.73.2;
id437  Cuff    stem    57936   79026   1000    +       .       ID=Abf022711.2;Parent=Abf022711;source_id=DUFF380.73.2;
id437  duff    main    57936   79026   1000    +       .       ID=Abf022721;source_id=DUFF380.73.2;
id437  Cuff    stem    57936   79026   1000    +       .       ID=Abf022721.2;Parent=Abf022721;source_id=DUFF380.73.2;

is it possible ?
# 2  
Old 07-27-2015
Making some wild assumptions about where the data for the Parent field is supposed to come from, perhaps something like:
Code:
awk '
$3 == "stem" {
	split($9, p, /[=.]/)
	sub(/;/, ";Parent=" p[2] ";")
}
1' file

As always, if you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk.
# 3  
Old 07-28-2015
Code:
$ perl -pe 's/(?<=\s)(ID=(\w+\d+)\.\d+;)/$1Parent=$2;/' test.file
id437  duff    main    57936   79026   1000    +       .       ID=Abf022711;source_id=DUFF380.73.2;
id437  Cuff    stem    57936   79026   1000    +       .       ID=Abf022711.2;Parent=Abf022711;source_id=DUFF380.73.2;
id437  duff    main    57936   79026   1000    +       .       ID=Abf022721;source_id=DUFF380.73.2;
id437  Cuff    stem    57936   79026   1000    +       .       ID=Abf022721.2;Parent=Abf022721;source_id=DUFF380.73.2;

or
Code:
perl -pe 's/(ID=(\w+\d+)\.\d+;)/$1Parent=$2;/ if /stem/' test.file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to change name of the file with first line of the file which has some unwanted text in it?

I have a log file, which i have divided into 14 files using csplit, the file looks like below test-000000 test-000001 #and so on until 14 now I want all the 14 files generated to be renamed as the some part of test in first line of the file how can i eliminate the unwanted text? sample... (5 Replies)
Discussion started by: Sekhar419
5 Replies

2. Shell Programming and Scripting

Trying to take file numbers from a file, pass them to sed to change strings in corresponding lines

I have a bunch of file numbers in the file 'test': I'm trying the above command to change all the instances of "H" to "Na+" in the file testsds.pdb at the line numbers indicated in the file 'test'. I've tried the following and various similar alternatives but nothing is working: cat test |... (3 Replies)
Discussion started by: crunchgargoyle
3 Replies

3. Shell Programming and Scripting

Change the file name and copy old file content to new file names.

Hi, I have a files in a directory as below :- ls -1 mqdepth-S1STC02 proc-mq-S1STC01 proc-mq-S1STC02 proc-mq-S1STC03 Whereever i have S1STC i need to copy them into new file with file name S2STC. expected output :- ls -1 mqdepth-S2STC02 proc-mq-S2STC01 proc-mq-S2STC02... (3 Replies)
Discussion started by: satishmallidi
3 Replies

4. UNIX for Dummies Questions & Answers

How to change crontab entries?

How to edit crontab entries . A job is scheduled as 35 15 * * * it.sh rahul_raj/hdhd i want to change it as 45 15 * * * it.sh rahul_raj/hdhd 1.crontab -e 2.shift+g ---go to last line .press enter.Press <i> . After this what should i do? (6 Replies)
Discussion started by: rafa_fed2
6 Replies

5. Shell Programming and Scripting

Help i want to change the data of one file and apend it into 2nd file.

Hi All, Please help i have written an ksh script, where i am actually take count of lines in one file and want to update this count to 2nd field of a new file and apend the this into an existing file. Note the below script is in for loop -------- I am apending few records in a file... (7 Replies)
Discussion started by: iamnoone
7 Replies

6. Shell Programming and Scripting

ksh; Change file permissions, update file, change permissions back?

Hi, I am creating a ksh script to search for a string of text inside files within a directory tree. Some of these file are going to be read/execute only. I know to use chmod to change the permissions of the file, but I want to preserve the original permissions after writing to the file. How can I... (3 Replies)
Discussion started by: right_coaster
3 Replies

7. Shell Programming and Scripting

how to change the current file processing to some other random file in awk ?

Hello, say suppose i am processing an file emp.dat the field of which are deptno empno empname etc now say suppose i want to change the file to emp.lst then how can i do it? Here i what i attempted but in vain BEGIN{ system("sort emp.dat > emp.lst") FILENAME="emp.lst" } { print... (2 Replies)
Discussion started by: salman4u
2 Replies

8. UNIX for Dummies Questions & Answers

How to change the file modification time of a file on nfs mount point

Hi I am accessing a file on nfs mounted device, after completing using of the file, i am tring to restore the access time and modification times of the file. So i got the previous modified time of the file using stat() function and trying to set the date and time for the file, To set these... (6 Replies)
Discussion started by: deepthi.s
6 Replies

9. Shell Programming and Scripting

how to change crontab entries

Hi Friends, I need to change crontab entries in prod. $crontab -l -> using this i can see the entries only Plese tell me how to edit this crontab and how to change the entires Waiting for ...... Thanks In advance friends Krish. (2 Replies)
Discussion started by: kittusri9
2 Replies

10. UNIX for Dummies Questions & Answers

Help with multiple file rename - change case of part of file name

Hi there, I hope someone can help me with this problem : I have a directory (/var/www/file/imgprofil) which contains about 10000 JPG files. They have a naming convention thus : prefix-date-key-suffix.jpg they all have the prefix p-20050608- then AAAA is a 4 letter code the suffix is... (7 Replies)
Discussion started by: steve7
7 Replies
Login or Register to Ask a Question