Print a Search Pattern after modification


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print a Search Pattern after modification
# 1  
Old 06-08-2011
Print a Search Pattern after modification

Using either vim or awk or sed

If I wish to to search for an unknown pattern - lets say 1B2495 or 1Q2345

so Search pattern : 1[A-Z][0-9][0-9][0-9][0-9]

and replace the 1 with 2 to print out :

2B2495 or 2Q2345

what are the possible commands.

Struggling here - help would be appreciated.
# 2  
Old 06-08-2011
Sed:

Code:
sed 's/1\([A-Z][0-9][0-9][0-9][0-9]\)/2\1/g' file

This User Gave Thanks to clx For This Post:
# 3  
Old 06-08-2011
alternate pattern..
Code:
 /^[1-2][A-Z][0-9]\{4\}$/

# 4  
Old 06-08-2011
Brilliant - works perfectly thank you.
# 5  
Old 06-08-2011
Michaelrozar17 ~ how does this one work ?
# 6  
Old 06-08-2011
Its another version of anchal_khare's solution. The above given pattern in post# 3 matches 2B2495 or 2Q2345 or 1B2495 or 1Q2345. I didn notice that you need to change 1 to 2. Below is the modified one. Instead of writing [0-9] four times we code as [0-9]\{4\} which also means the same.
Code:
sed 's/^1\([A-Z][0-9]\{4\}$\)/2\1/' inputfile > outfile

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

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search Pattern and Print lines in Single Column

Hi Experts I have small query where I request the into a single file Suppose: File1: {Unique entries} AA BB CC DD FileB: AA, 123 AA, 234 AA, 2345 CC, 123 CC, 5678 DD,123 BB, 7890 (5 Replies)
Discussion started by: navkanwal
5 Replies

2. Shell Programming and Scripting

Need one liner to search pattern and print everything expect 6 lines from where pattern match made

i need to search for a pattern from a big file and print everything expect the next 6 lines from where the pattern match was made. (8 Replies)
Discussion started by: chidori
8 Replies

3. Shell Programming and Scripting

pattern search and print using sh

Hi All, Have a file contains multiple line with the following file.txt insert: akdkas job:ksdjf command: aldfasdf asdfsdfa asdfas.sh machine: asdfa need to grep for insert and machine and print only "akdkas,asdfa" cat file.txt | egrep "insert|machine" | awk -F: '{print $2}' output ... (5 Replies)
Discussion started by: uniqme
5 Replies

4. Shell Programming and Scripting

Print a pattern between the xml tags based on a search pattern

Hi all, I am trying to extract the values ( text between the xml tags) based on the Order Number. here is the sample input <?xml version="1.0" encoding="UTF-8"?> <NJCustomer> <Header> <MessageIdentifier>Y504173382</MessageIdentifier> ... (13 Replies)
Discussion started by: oky
13 Replies

5. Shell Programming and Scripting

Awk/Sed: Search Pattern from file and Print

Hi, I want to search for patterns (from a file) in a file and print the line matching the patterns and the line before it. I have to search for 100s of patterns from a file. Any help with AWK or Sed. Thanks! (2 Replies)
Discussion started by: saint2006
2 Replies

6. Shell Programming and Scripting

Search for Pattern and Print including Lines in between

Gurus, I have a big file that needs to be sorted out and I cant figure out what to do. The file name is as below: Name: xxxx yyyy nnnn Description: dfffgs sdgsgsf hsfhhs afgghhjdgj fjklllll gsfhfh Updated: jafgadsgg gsg Corrected: date today The file consists of line like these. ... (13 Replies)
Discussion started by: The One
13 Replies

7. UNIX for Dummies Questions & Answers

Print lines between the search pattern

hi, I have a file say x.txt containing xxx 123 bla bla ... you xxx dfk dbf ... me xxx ... ... keeps on.. i need to search for pattern in the line starting xxx in the file. If pattern matched, I need to fetch all the lines till i find next xxx. (17 Replies)
Discussion started by: prsshini
17 Replies

8. Shell Programming and Scripting

Print the line within the search pattern

Hi Guys, I had file as typedef struct { char TrailerType1; char TrailerTxt1; }Trailer; typedef struct { char PfigMoneyType; char PfigMoneyvalue; }PfigMoney; i need to print the lines within the search pattern. if i give the search pattern as... (3 Replies)
Discussion started by: manosubsulo
3 Replies

9. Shell Programming and Scripting

awk how to print if the search pattern contains speace

the data file is as below: > cat master.cnf /usr| location for usr|5 /src/ver1| version 1 |10 /src/ver2/log| ver 2 log |25 /src/sys/apps/log| Application log for sys|36 /src/sys/apps/conf| configuration location for app|45 /src/sys/apps/bin| binary location app|55my script is as below: ... (1 Reply)
Discussion started by: McLan
1 Replies
Login or Register to Ask a Question