Join two commands sed and grep


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Join two commands sed and grep
# 1  
Old 07-30-2013
Join two commands sed and grep

Hi all,

I have two separate commands which I would like to join. Basically, I want to match a line and insert a character at the end of the previous line to the matched line

Here is what I have got
Code:
grep -B1 '^>'
sed 's/$/*/'

Any help is much appreciated thanks
# 2  
Old 07-30-2013
if i understand your requirement correctly, you want to search for a line with matching pattern and append something. you can try this

im just appending string 'matched' to the end of the line.

Code:
[oracle@server ~]$ cat infile
1
2
3
4
5
[oracle@server ~]$ sed 's/5.*/&matched/g' infile
1
2
3
4
5matched
[oracle@server ~]$ sed 's/2.*/&matched/g' infile
1
2matched
3
4
5

# 3  
Old 07-30-2013
Code:
$ cat f
line one
line to match PATTERN
last line

Code:
$ awk 'b{if($0~/PATTERN/)b=b"@@";print b}{b=$0}END{print b}' f
line one@@
line to match PATTERN
last line

# 4  
Old 07-30-2013
Hi riyandgreat 25 and ripat,

Riyandgreat 25 - I want to append a character to the line before the matched line and not the matched line. Thanks so much

Ripat - your solution worked great. Thanks
# 5  
Old 07-30-2013
Note that ripat's awk script will delete all empty lines and all lines that consist entirely of a string of "0" characters.

The following ed script doesn't ignore empty lines and doesn't ignore lines that only contain a string of "0"s. Note that the patterns accepted by awk (which uses extended regular expressions) and ed (which uses basic regular expressions) are slightly different. If that difference doesn't matter to you, you can try this simple ed script:
Code:
#!/bin/ksh
FILE=${1:-f}
PATTERN=${2:-PATTERN}
REPLACE=${3:-@@}
ed -s "$FILE" <<-EOF
	2,\$g/$PATTERN/.-1s/$/$REPLACE/
	1,\$p
	Q
EOF

With the file f containing a few more lines than were in ripat's example:
Code:
PATTERN on line1
line two
line to match PATTERN
last line

PATTERN after empty line
000
PATTERN after 000
0
PATTERN after 0
 
PATTERN after blank line

where the next to the last line just contains a space character, the ed script above produces the output:
Code:
PATTERN on line1
line two@@
line to match PATTERN
last line
@@
PATTERN after empty line
000@@
PATTERN after 000
0@@
PATTERN after 0
 @@
PATTERN after blank line

while ripat's awk script produces the output:
Code:
PATTERN on line1
line two@@
line to match PATTERN
last line
PATTERN after empty line
PATTERN after 000
PATTERN after 0
 @@
PATTERN after blank line

If your pattern or the string to be added to lines before those matching the pattern contain any slash (/) characters, replace the slashes in the ed g and s commands with a character (other than a newline character) that will never appear in your pattern or added text. I frequently use a control-G for this when processing files that don't contain any ASCII BEL (sometimes called alert) characters (it shows up nicely when I'm editing the script with vi, but it is sometimes hard to describe in text to naive users).

If the difference between BREs and EREs matters, the following awk script will produce the same output as the ed script above. With any of these scripts, if the regular expression contains any meta-characters used in the type of RE you're using, they will have to be escaped appropriately. If you're using ed, you also have to consider what can happen with ampersand (&) and backslash digit (\x 1 <= x <= 9) in the replacement string. With both ed and awk, special attention will also be required if the pattern and replacement strings contain any dollar signs ($).
Code:
#!/bin/ksh
FILE=${1:-f}
PATTERN=${2:-PATTERN}
REPLACE=${3:-@@}
awk -v pat="$PATTERN" -v rep="$REPLACE" '
FNR>1 { if($0 ~ pat)    print last rep
        else            print last
}
{       last = $0 }
END {   print last }' "$FILE"

As always when writing new awk scripts, if you are using a Solaris/SunOS system, use /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk instead of /bin/awk or /usr/bin/awk.

Note that although I used the Korn shell to test these scripts, any POSIX conforming shell will work. If you don't need the script to provide default values for the file to be processed, the pattern to use, and the string to be added to the lines prior to the lines containing the pattern, you can easily hard wire those values into either of the above scripts and use any shell you want.

Hope this helps...
This User Gave Thanks to Don Cragun For This Post:
# 6  
Old 07-30-2013
Quote:
Originally Posted by Don Cragun
Note that ripat's awk script will delete all empty lines and all lines that consist entirely of a string of "0" characters.
True. Just adapted my snippet.

Code:
$ awk 'NR>1{if($0~/PATTERN/)b=b"@@";print b}{b=$0}END{print b}' f

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Join lines using sed or awk

Hi, I have text file that looks like this: blabla bla PATTERN LINE1 LINE2 bla bla bla PATTERN LINE1 LINE2 bla PATTERN LINE1 LINE2 bla (9 Replies)
Discussion started by: hench
9 Replies

2. Shell Programming and Scripting

Join the line on delimiter using sed/awk in UNIX

I've input as , abcd| ef 123456| 78| 90 Desired output as, abcdef 1234567890 Anyone please give the solution. (5 Replies)
Discussion started by: jinixvimal
5 Replies

3. UNIX for Dummies Questions & Answers

sed, join lines that do not match pattern

Hello, Could someone help me with sed. I have searched for solution 5 days allready :wall:, but cant find. Unfortunately my "sed" knowledge not good enough to manage it. I have the text: 123, foo1, bar1, short text1, dat1e, stable_pattern 124, foo2, bar2, long text with few lines, date,... (4 Replies)
Discussion started by: petrasl
4 Replies

4. Shell Programming and Scripting

Using a combination of sort/cut/grep/awk/join/paste/sed

I have a file and need to only select users that have a shell of “/bin/bash” in the line using awk or sed please help (4 Replies)
Discussion started by: boyboy1212
4 Replies

5. Homework & Coursework Questions

find grep sed commands homework

Use and complete the template provided. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I have to make as home work several commands with gerp find and sed 2. Relevant commands, code, scripts, algorithms: FIND command -use command find... (8 Replies)
Discussion started by: ViruS89
8 Replies

6. Shell Programming and Scripting

How to use SED to join multiple lines?

Hi guys, anyone know how can i join multiples lines using sed till the end of a file and output to another file in a single line? The end of each line will be replaced with a special char "#". I am using the below SED command, however it seems to remove the last 2 lines. Also not all lines... (12 Replies)
Discussion started by: DrivesMeCrazy
12 Replies

7. UNIX for Dummies Questions & Answers

Join 2 files with multiple columns: awk/grep/join?

Hello, My apologies if this has been posted elsewhere, I have had a look at several threads but I am still confused how to use these functions. I have two files, each with 5 columns: File A: (tab-delimited) PDB CHAIN Start End Fragment 1avq A 171 176 awyfan 1avq A 172 177 wyfany 1c7k A 2 7... (3 Replies)
Discussion started by: InfoSeeker
3 Replies

8. Shell Programming and Scripting

Join, GREP

Hi All, I have 2 file. ACC_NUM contains only account numbers & ACC_DETAIL contains all information including account number seperated by ~ delimiter. i am serching the account number in ACC_DETAIL from ACC_NUM. If it is exist, then copy all information in ACC_DETAIL_NEW file. For that i... (7 Replies)
Discussion started by: Amit.Sagpariya
7 Replies

9. Shell Programming and Scripting

awk, join or sed

$ cat file1 a:23:43 A B C a:24:21 a:23:44 S D A F a:24:44 a:23:45 S D E a:24:45 $ cat file2 a:23:53 (1 Reply)
Discussion started by: jkl_jkl
1 Replies

10. Shell Programming and Scripting

to join the corresponding lines using shell commands or awk

Suppose u have this file gi_1 ABCDEFDHIJ KMNOPQRSTU VWXYZABCDE gi_2 JKLMNOPQRS TUVWXYZABC DEFGHIJKLM gi_3 PQRSTUVWXY ZABCDEFGHI JKLMNOPQRS gi_4 CDEFGHIJKL MNOPQRSTUV WXYZABCDEF gi_5 IJKLMNOPQR STUVWXYZAB CDEFGHIJKLM FGHIJKLMNO PQRSTUVWXY ZABCDEFABC NOPQRSTUVW XYZABCDEFG... (7 Replies)
Discussion started by: cdfd123
7 Replies
Login or Register to Ask a Question