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.

Sed:

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

alternate pattern..

 /^[1-2][A-Z][0-9]\{4\}$/

Brilliant - works perfectly thank you.

Michaelrozar17 ~ how does this one work ?

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.

sed 's/^1\([A-Z][0-9]\{4\}$\)/2\1/' inputfile > outfile
1 Like