matching a pattern in a file & prefixing a word


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting matching a pattern in a file & prefixing a word
# 1  
Old 04-29-2009
CPU & Memory matching a pattern in a file & prefixing a word

Hi,

In my shell script i have to match a patten in a file , if found i have to prefix the entair line by a "word"

eg. pattern = "aaa" prefix= #123
file: bbbb xxx
zzzz aaaa
qqqq kkkk

outPut file: bbbb xxx
#123zzzz aaaa
qqqq kkkk

Waiting your precious reply.....
# 2  
Old 04-29-2009
The not so precious suggestion would be:
Code:
sed '/aaa/ s/^/#123/' myFile

# 3  
Old 04-29-2009
CPU & Memory i need its reverse procedure

Quote:
Originally Posted by vgersh99
The not so precious suggestion would be:
Code:
sed '/aaa/ s/^/#123/' myFile

First of Thank you,......I also need to delete that #123 where pattern "aaa" is there
pattern = "aaa" prefix= #123
file: bbbb xxx
#123zzzz aaa
qqqq kkkk


file: bbbb xxx
zzzz aaaa
qqqq kkkk
# 4  
Old 04-29-2009
You could have experimented a bit......
Code:
echo '#123 asdfjkh aaa sdfkj' | sed 's/^\(#123\)\(.*aaa.*\)/\2/'

# 5  
Old 04-29-2009
CPU & Memory Thank you.....

Quote:
Originally Posted by vgersh99
You could have experimented a bit......
Code:
echo '#123 asdfjkh aaa sdfkj' | sed 's/^\(#123\)\(.*aaa.*\)/\2/'

Thank you , your code works fine......
By the way in ur reply gap.....i designed another code that's equivalent to yours

grep aaa a.c | sed 's/#123//'

Thankyou very much .......bye
# 6  
Old 04-29-2009
Quote:
Originally Posted by shivarajM
Thank you , your code works fine......
By the way in ur reply gap.....i designed another code that's equivalent to yours

grep aaa a.c | sed 's/#123//'

Thankyou very much .......bye
Sorry, I don't understand what's being said here.......
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script example to Grep matching word from file

Hi expert, Need help in shell script. a.txt file output is below i would like to grep 3 line and 1st column value which is admin\22226 only and not full line. i only know admin word as 22226 can come anything with admin\ in file. also after fetching it i would like to use this... (1 Reply)
Discussion started by: kuljeetpal
1 Replies

2. Shell Programming and Scripting

Big pattern file matching within another pattern file in awk or shell

Hi I need to do a patten match between files . I am new to shell scripting and have come up with this so far. It take 50 seconds to process files of 2mb size . I need to tune this code as file size will be around 50mb and need to save time. Main issue is that I need to search the pattern from... (2 Replies)
Discussion started by: nitin_daharwal
2 Replies

3. Shell Programming and Scripting

Pattern Matching & replacing of content in file1 with file2

I have file 1 & file 2 with content mentioned below. I want to get the output as shown in file3. Requirement: check the content of column 1 & column 2, if value of column 1 in file1 matches with first column of file2 then remaining columns(2&3) of file2 should get replaced, also if value of... (4 Replies)
Discussion started by: siramitsharma
4 Replies

4. Shell Programming and Scripting

Pattern matching & storing result in variable

Hi! i'm trying to parse textfiles against a pattern and storing the result in a variable. The strings i want to get are embraced by and can occur several times in one line, so e.g. some text anything else endwhat i have so far: #!/bin/bash for f in $* do exec 3<&0 exec 0<$f ... (2 Replies)
Discussion started by: thoni
2 Replies

5. Shell Programming and Scripting

AWK - Pattern Matching & Replacing - Performance

Experts, I am a beginner to Unix Shell Scripting We have source as a flat file which contains CTRL+F character as the delimiter. We need to count the number of records in the file (CTRL+F) to perform file validation Following command being used: awk '{cnt+=gsub(//,"&")}END {print cnt}'... (4 Replies)
Discussion started by: srivijay81
4 Replies

6. Shell Programming and Scripting

To read data word by word from given file & storing in variables

File having data in following format : file name : file.txt -------------------- 111111;name1 222222;name2 333333;name3 I want to read this file so that I can split these into two paramaters i.e. 111111 & name1 into two different variables(say value1 & value2). i.e val1=11111 &... (2 Replies)
Discussion started by: sjoshi98
2 Replies

7. UNIX for Dummies Questions & Answers

grep only word matching the pattern

Hi gurus, A file contains many words in format "ABC.XXXX.XXXX.X.GET.LOG" (X->varying). Now my shell script want this list (only words in formatABC.XXXX.XXXX.X.GET.LOG ) to continue the process. Pls help me. Thanks, Poova. (8 Replies)
Discussion started by: poova
8 Replies

8. Shell Programming and Scripting

Extracting the strings matching a pattern from a word

Hi All , I need to extract the strings that are matching with the pattern : CUST.<AnyStringOfAnyLength>.<AnyStringOfAnyLength> from a file and then write all these string into another file. e.g. If a file SOURCE contains following lines : IF(CUST.ABCD.EFGH==1) THEN CUST.ABCD.EFGH =... (7 Replies)
Discussion started by: swapnil.nawale
7 Replies

9. UNIX for Dummies Questions & Answers

Pattern matching New to Sed & Awk

Hello, Despite reading the Pattern Matching chapter in the O'Reilly Sed & Awk book several times and looking at numerous examples, I cannot seem to get any kind of conditional script to work in my awk scripts! I am able to do the basic awk and grep script to capture the data but when I do with... (0 Replies)
Discussion started by: pg55
0 Replies

10. Shell Programming and Scripting

Pattern matching for file

Hi All, I'm new to perl, My requirement is to check if particular file exists. e.g. filename.txt, filename1.txt, filename2.txt etc I tried the below code:- my $var1 = "filename.txt" if ( -e ($var1 = ~ /file\w/)) { print "File found \n"; } else { print "File not found \n"; } ... (0 Replies)
Discussion started by: doitnow
0 Replies
Login or Register to Ask a Question