grep particular value in files and change it to different value


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grep particular value in files and change it to different value
# 1  
Old 03-24-2009
grep particular value in files and change it to different value

Hello all
Need some help in doing this.Im trying to grep particular value(pid=34) from 'n' number of files and change it to different value(pid_num=34).Is there any way we can do it with a single command.Please advise.

Ex:grep pid *.txt
1.txt ;pid=34
2.tx ;pid=34
3.txt ;pid=34
...... n.txt

Now I want to convert the pid=34 to pid_num=34 in all the previously mentioned files (1.txt,2.txt.....n.txt)

o/p:
1.txt ;pid_num=34
2.txt ;pid_num=34
.....n.txt

Thanks
Kev
# 2  
Old 03-24-2009
Quote:
Originally Posted by coolkid
Hello all
Need some help in doing this.Im trying to grep particular value(pid=34) from 'n' number of files and change it to different value(pid_num=34).Is there any way we can do it with a single command.Please advise.

Ex:grep pid *.txt
1.txt ;pid=34
2.tx ;pid=34
3.txt ;pid=34
...... n.txt

Now I want to convert the pid=34 to pid_num=34 in all the previously mentioned files (1.txt,2.txt.....n.txt)

o/p:
1.txt ;pid_num=34
2.txt ;pid_num=34
.....n.txt

Thanks
Kev
hmm it may work try
Code:
 
awk '/pid/&&split($0,A,"="){print FILENAME":"A[1]"_num="A[2]}' *.txt

# 3  
Old 03-24-2009
Thanks for the help but no luck...when i run the command it is showing the value beside it but the change is not happening..

o/p:
1.txt ;pid=34 pid_num=34

when i run this "grep pid *.txt" it is still showing as pid instead of pid_num
# 4  
Old 03-24-2009
its working fine for me
Code:
fnsonlu1-/home/fnsonlu1> cat vv
pid=34
fnsonlu1-/home/fnsonlu1> cat vvv
pid=34
fnsonlu1-/home/fnsonlu1>  awk '/pid/&&split($0,A,"="){print FILENAME":"A[1]"_num="A[2]}' vv*
vv:pid_num=34
vvv:pid_num=34

# 5  
Old 03-24-2009
Thank you vidyadhar85 i guess i made a typo..can you also help me with changing "pid" to "sid" in all files please.

Appreciate your help
Kev
# 6  
Old 03-24-2009
Quote:
Originally Posted by coolkid
Thank you vidyadhar85 i guess i made a typo..can you also help me with changing "pid" to "sid" in all files please.

Appreciate your help
Kev
Code:
sed 's/pid/sid/g' filename>new_file

if you have -i option
Code:
 
sed -i 's/pid/sid/g' filename

# 7  
Old 03-24-2009
Awesome thank you so much for your help man..it is working now..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Python - glob () - How to grep same files with different extension files

Hi I Have a directory and i have some files below abc.txt abc.gif gtee.txt ghod.pid umni.log unmi.tar How can use glob function to grep abc files , i have created a variable "text" and i assigned value as "abc", please suggest me how can we use glob.glob( ) to get the output as below... (2 Replies)
Discussion started by: kumar85shiv
2 Replies

2. Shell Programming and Scripting

Change the content of files but not change the date

I have 100 files in a directory , all the files have a word "error" and they are created in different date . Now I would like to change the word from "error" to "warning" , and keep the date of the files ( that means do not change the file creation date after change the word ) , can advise what can... (7 Replies)
Discussion started by: ust3
7 Replies

3. Shell Programming and Scripting

Change the content of files but not change the date

I have 100 files in a directory , all the files have a word "error" and they are created in different date . Now I would like to change the word from "error" to "warning" , and keep the date of the files ( that means do not change the file creation date after change the word ) , can advise what can... (0 Replies)
Discussion started by: ust3
0 Replies

4. Shell Programming and Scripting

sed change in all files

I have a list of property file can each file has name=value kind of key value pair can replace all = with say ; in each file using sed or any other command (4 Replies)
Discussion started by: Vinayshell
4 Replies

5. Shell Programming and Scripting

Find required files by pattern in xml files and the change the pattern on Linux

Hello, I need to find all *.xml files that matched by pattern on Linux. I need to have written the file name on the screen and then change the pattern in the file just was found. For instance. I can start the script with arguments for keyword and for value, i.e script.sh keyword... (1 Reply)
Discussion started by: yart
1 Replies

6. Shell Programming and Scripting

Change files names

HI, Is there any possibility to change names of many files using "mv" and "for" loop ??? For ex. I have many txt files. file1.txt file2.txt ... And I tried something like this, but it's not working for (( i=1 ; $i<=10 ; i++ )) ; do mv file$i.txt newfile$1.txt; done (3 Replies)
Discussion started by: Physix
3 Replies

7. Shell Programming and Scripting

grep for certain files using a file as input to grep and then move

Hi All, I need to grep few files which has words like the below in the file name , which i want to put it in a file and and grep for the files which contain these names and move it to a new directory , full file name -C20091210.1000-20091210.1100_SMGBSC3:1000... (2 Replies)
Discussion started by: anita07
2 Replies

8. Shell Programming and Scripting

This script cut size of files "Lol" change string in files

Basic: find . -type f -name “*.txt” -print | awk '{gsub("Ontem", "AntesdeOntem", $0); print > FILENAME}' *.txt The idea is in folder /home/myapontamentos I have some files and i need to change in all them the word "ontem" to "antesdeontem". But bigger files are cut (size i mean)... (4 Replies)
Discussion started by: single
4 Replies

9. Shell Programming and Scripting

how to grep word from .gz files & .z files

Hi I have find some account numbers from .gz files. There thousands of .gz files, also i am unable to grep .gz / .z files. I want to find file names & and those lines from list. Could you pls tell me / give me any syntax for finding word from ,gz files using grep? Thanks in advance (8 Replies)
Discussion started by: udaya_subbu
8 Replies

10. UNIX for Dummies Questions & Answers

Change field separator of grep from : to space

Hi, All, I wonder how to change the field separator of grep from : to space when use grep to display. for example when I use grep -e 'pattern1' -e 'pattern2' -n filename to find patterns, it use : to separate patterns, but I want to use blank space. is there an option I can set to... (2 Replies)
Discussion started by: Jenny.palmy
2 Replies
Login or Register to Ask a Question