awk to insert missing string based on pattern in file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk to insert missing string based on pattern in file
# 1  
Old 03-22-2017
awk to insert missing string based on pattern in file

Using the file below, which will always have the first indicated by the digit after the -
and last id in it, indicated by the digit after the -, I am trying to use awk
to print the missing line or lines in file following the pattern of the previous line.
For example, in the file below the next id to print using thr previous line would be RETT-02,
since the previous is RETT-01. Since the last line in the file is 03, that is
the only missing line, however the next time it may be different (with more lines).

The awk below works great as long as I enter the values to use, but is there a way to automaticaly set these? Thank you Smilie.

Code:
RETT-01
RETT-03

Code:
awk '/RETT-03/{print "RETT-02"}1' file

RETT-01
RETT-02
RETT-03

# 2  
Old 03-22-2017
If you know the first and last ID, why not generate the sequence yourself:
Code:
[user@host ~]$ seq -f "RETT-%.0f" 1 5
RETT-1
RETT-2
RETT-3
RETT-4
RETT-5

These 3 Users Gave Thanks to balajesuri For This Post:
# 3  
Old 03-22-2017
Try:
Code:
awk -F- 'NR>1 && $2>p+1{for(i=p+1; i<$2; i++) printf "%s-%02d\n",$1,i} {p=$2}1' file


---
variation, irrespective of the length of number..
Code:
awk -F- 'NR>1 && $2>p+1{for(i=p+1; i<$2; i++) printf "%s-%0*d\n",$1,length(p),i} {p=$2}1' file


Last edited by Scrutinizer; 03-22-2017 at 02:58 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 03-22-2017
Hello cmccabe,

Could you please try following and let me know if this helps you.
Let's say we have following Input_file:
Code:
cat Input_file
RETT-01
RETT-03
RETT-11
RETT-13
RETT-33
RETT-34
RETT-34

Then following is the code for same.
Code:
awk -F"-" 'NR==1{print;val=$2;next} ($2-val>1){while($2-val>1){val++;printf("%s%02d\n",$1 FS, val);};print;val=$2;next} {val=$2;print}'  Input_file

Hello Scrutinizer,

Sorry my code looks very similar to you but I was on this page for quite sometime and didn't know you have replied already to it, though mine one of slightly different. Thought to let you know on same.

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 5  
Old 03-24-2017
Thank you all Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to update value based on pattern match in another file

In the awk, thanks you @RavinderSingh13, for the help in below, hopefully it is close as I am trying to update the value in $12 of the tab-delimeted file2 with the matching value in $1 of the space delimeted file1. I have added comments for each line as well. Thank you :). awk awk '$12 ==... (10 Replies)
Discussion started by: cmccabe
10 Replies

2. Shell Programming and Scripting

Insert line based on found string

Hi All I'm trying to insert a new line at the before each comment line in a file. Comment lines start with '#-----' there are other comments with in lines but I don't want a new line there. Example file: blah blah #do not insert here #this is a comment blah #some more #another comment... (10 Replies)
Discussion started by: Mudshark
10 Replies

3. Shell Programming and Scripting

Insert data based on pattern

if it is finding some data based on pattern 'test' then insert else if has no data based on the pattern 'test' then exit successfully cat file | grep test > file2 (3 Replies)
Discussion started by: jagu
3 Replies

4. UNIX for Dummies Questions & Answers

How to combine and insert missing consecutive numbers - awk or script?

Hi all, I have two (2) sets of files that are based on some snapshots of database that I want to merge and insert any missing sequential number. Below are example representation of these files: file1: DATE TIME COL1 COL2 COL3 COL4 ID 01/10/2013 0800 100 ... (3 Replies)
Discussion started by: newbie_01
3 Replies

5. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

6. Shell Programming and Scripting

How to cut string and find missing pattern?

i have list in file named sample.txt eg i want to cut the 3rd and 4th character i.e. 01,02,03....,24(max length is 24) and i want to find the missing sequence .and display them i.e. (15 Replies)
Discussion started by: sagar_1986
15 Replies

7. Shell Programming and Scripting

Insert missing field using perl,sed,awk

sample file (comma as field separators) MessageFlow,1,BusIntBatchMgr,a OOBEvent,1,BusIntBatchMgr,a TaskEvents,1,,a MTTrace,1,,a MTWarning,,1,a MessageFlow,1,Batch,a OOBEvent,1,Batch,a TaskEvents,1,,a EAISAPIdocWizard,1,BusIntMgr,a EAISAPBAPIWizard,1,BusIntMgr,a... (3 Replies)
Discussion started by: vrclm
3 Replies

8. Shell Programming and Scripting

How do i find the first number in each line and insert dummy string into the missing columns?

Hi, I have one input file with the following content: MY_inpfile.txt Aname1 Cname1 Cname2 1808 5 Aname2 Cname1 1802 47 Bname1 ? 1819 22 Bname2 Cname1 1784 11 Bname3 1817 9 Zname1 Cname1 1805 59 Zname2 Cname1 Cname2 Cname3 1797 27 Every line in my input file have a 4 digit... (5 Replies)
Discussion started by: Szaffy
5 Replies

9. Shell Programming and Scripting

how to get data from hex file using SED or AWK based on pattern sign

I have a binary (hex) file I need to parse to get some data which are encoded this way: .* b4 . . . 01 12 .* af .* 83 L1 x1 x2 xL 84 L2 y1 y2 yL By another words there is a stream of hexadecimal bytes (in my example separated by space for better readability). I need to get value stored in... (3 Replies)
Discussion started by: sameucho
3 Replies

10. Shell Programming and Scripting

Retrieve string from each line in a file based on a pattern in Unix

I have a file which contains several lines. Sample content of the file is as below. OK testmessage email<test@123> NOK receivemessage email<123@test> NOK receivemessage email(123@test123) NOK receivemessage email<abc@test> i would like to know by scripting will... (10 Replies)
Discussion started by: ramasar
10 Replies
Login or Register to Ask a Question