Place digit in front of the line searching pattern using sed command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Place digit in front of the line searching pattern using sed command
# 1  
Old 04-14-2011
Place digit in front of the line searching pattern using sed command

hi All,

i want to add the single digit front of the line in the report file and string compare with pattern file.

patter file: pattern1.txt

pattern num
like 4
love 3
john 2

report file: report.txt

i like very much
but john is good boy
i will love u

so after execute the script output is report.txt file like

4 i like very much
2 but john is good boy
3 i will love u

---------- Post updated at 08:18 PM ---------- Previous update was at 12:52 PM ----------

Please update anyone using by sed command...

Last edited by krbala1985; 04-14-2011 at 10:14 PM.. Reason: typo error.
# 2  
Old 04-14-2011
What the output, if have two and more patterns found in one line?
Code:
i like john very much

And why it needs be sed command?
# 3  
Old 04-14-2011
hi ,

Please check now. what i want na ...i have a one report file and pattern file.
i get the pattern from pattern file and compare to report file if matchine i get the line and place the equal digit to front of the line in the same report file.

patter file: pattern1.txt

pattern num
like 4
love 3
john 2

report file: report.txt

i like very much
but john is good boy
i will love u

output -> report.txt file like

4 i like very much
2 but john is good boy
3 i will love u
# 4  
Old 04-14-2011
Your still don't answer my question:

What's the expect output, if have two and more patterns found in one line?

Code:
i like john very much

something like:

Code:
4 2 i like john very much
or 
4 i like john very much
or 
2 i like john very much

# 5  
Old 04-14-2011
output similar like.....

4 i like john very much

Sorry for miscommunication....Thanks in advance
# 6  
Old 04-15-2011
Code:
awk 'NR==FNR{a[$1]=$2;next}
{for(i=1;i<=NF;i++) if($i in a) print a[$i] FS $0}' pattern report
4 i like very much
2 but john is good boy
3 i will love u

This User Gave Thanks to yinyuemi For This Post:
# 7  
Old 04-15-2011
really Thank you....can u explain the command.

another thing i need to update the output report file itself.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

sed command that deletes lines with 3-5 digit strings

Hello! My final exam in my Linux class is tomorrow, and I was just reviewing the grep and sed commands. I came across an example, by which I got stumped: it asks to provide a sed command that deletes all lines that contain exactly 3 to 5 digit strings, from a file. In this case, I created a... (3 Replies)
Discussion started by: kalpcalp
3 Replies

2. Shell Programming and Scripting

awk pattern matching in place of sed

I have written a script to parse data from some files on a Solaris 10 system and send the output to a CSV formatted file. The code snipped i am using to pull the data is as follows.... src_line=$(sed -n "/^search_pattern$/{=;}" $file) for i in $src_line do start_line1=$(( i + 9 )) nawk -v... (4 Replies)
Discussion started by: electricheadx
4 Replies

3. Shell Programming and Scripting

sed command to grep multiple pattern present in single line and delete that line

here is what i want to achieve.. i have a file with below contents cat fileName blah blah blah . .DROP this REJECT that . --sport 7800 -j REJECT --reject-with icmp-port-unreachable --dport 7800 -j REJECT --reject-with icmp-port-unreachable . . . more blah blah blah --dport 3306... (14 Replies)
Discussion started by: vivek d r
14 Replies

4. Shell Programming and Scripting

problem using pattern with two digit in sed

Hi, I am trying to create a csv from a existing flat file.I am using the same data to create the csv file. But I have issues \10 columns onwards.. sed... (5 Replies)
Discussion started by: babom
5 Replies

5. Shell Programming and Scripting

how to add single digit in front of the word and line in the file.

Hi , how to add the single digit to front of the word and front of the lines in the one file with compare pattern file and get digit. like example pattern file pattern.txt pattern num bala 2 raja 3 muthu 4 File Name: chennai.dat muthu is good boy raja is bad boy selvam in super... (6 Replies)
Discussion started by: krbala1985
6 Replies

6. Shell Programming and Scripting

Searching a pattern in file and deleting th ewhole line containing the pattern

Hi All, Please can someone assist in the script I have made that searches a pattern in a file and delete the whole line containing the pattern. #!bin/sh # The pattern that user want to add to the files echo "Enter the pattern of the redirect" read value # check if the user has... (1 Reply)
Discussion started by: Shazin
1 Replies

7. Shell Programming and Scripting

Need help in sed command [ printing a pattern + its line no or line no alone ]

Hello friends, Only very recently i started learning sed command...an i found that sed is faster in finding the patterns than some of my scripts that uses grep to check the patten inside a file using line by line search method which is time consuming. The below script... (4 Replies)
Discussion started by: frozensmilz
4 Replies

8. Shell Programming and Scripting

Sed - Pattern Searching

Hi, Please take a look at the below eg. I would like to search for abc() pattern first and then search for (xyz) in the next line. If I can find the pattern, then I should delete the 3 lines. I can only find the pattern and delete but I am unable to find two patterns and delete. Any... (8 Replies)
Discussion started by: sreedevi
8 Replies

9. Shell Programming and Scripting

Need help in sed command (adding a blank line btw each block generated by pattern)

Hello friends, I have a C source code containing sql statements. I use the following sed command to print all the sql blocks in the source code.... sed -n "/exec sql/,/;/p" Sample.cpp The above sed command will print the sql blocks based on the pattern "exec sql" & ";"... (2 Replies)
Discussion started by: frozensmilz
2 Replies

10. UNIX for Dummies Questions & Answers

How to use cat command in sed pattern searching

Hi All, I have a file named pattern.dat which contains pattern like A1000090 250.00 250.00 i have one more file named test.dat in which this pattern is present. What i should do is, in test.dat after this pattern i should append comments. i used... (4 Replies)
Discussion started by: Sona
4 Replies
Login or Register to Ask a Question