del the line before the "abc"line and after it


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting del the line before the "abc"line and after it
# 1  
Old 03-23-2010
del the line before the "abc"line and after it

Code:
$file1
haha
hello i'm lei
i'm abc
you are right
yes it is

I want to get
Code:
file2
haha
i'm abc
yes it is

del line before and after the line contains abc I want to know the two steps 1,del the line before line "abc' ,2 del the line after the "abc


question 2
Code:
$file3
haha
hello i'm lei
i'm abc
you are right
yes it is

I want to get



$file4
Code:
haha
hello i'm lei
<!-- i'm abc -->
you are right
yes it is

then I want to recover it from file4 to file3

Code:
$file2
haha
hello i'm lei
i'm abc
you are right
yes it is

# 2  
Old 03-23-2010
is there a practical purpose, or is this homework

This appears to be structured like a homework problem, and not a real-life issue. Please explain how this is not homework.

Homework problems must appear in the proper forum and with course information provided - as per our forum rules.
# 3  
Old 03-24-2010
Quote:
Originally Posted by joeyg
This appears to be structured like a homework problem, and not a real-life issue. Please explain how this is not homework.

Homework problems must appear in the proper forum and with course information provided - as per our forum rules.

This is not homework, i just want to make the problem clear, so I express it like home work

Thanks

---------- Post updated at 08:31 PM ---------- Previous update was at 09:01 AM ----------

any answer experts
# 4  
Old 03-24-2010
Question 1:

Code:
n=$(grep -n "i'm abc" file1|cut -d: -f1)
echo $N
awk -v var=$n '!(NR==var-1||NR==var+1)' file1 > file2

Question 2:

Code:
sed "s/i'm abc/<-- i'm abc -->/" file3
sed "s/\(i'm abc\)/<-- \1 -->/" file3
awk '/i'"'"'m abc/ {$0="<--" $0 " -->"}1 ' file3

Question 3: (reverse file4 to file3)

Some solutions are ready for you, now it's your turn to answer it.
# 5  
Old 03-24-2010
qn1
Code:
sed -n '/abc/{x;s/\n.[^\n]*\n*$//;x;H;n;s/.*//g;n};/abc/!{H};${x;p}' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Failure: if grep "$Var" "$line" inside while read line loop

Hi everybody, I am new at Unix/Bourne shell scripting and with my youngest experiences, I will not become very old with it :o My code: #!/bin/sh set -e set -u export IFS= optl="Optl" LOCSTORCLI="/opt/lsi/storcli/storcli" ($LOCSTORCLI /c0 /vall show | grep RAID | cut -d " "... (5 Replies)
Discussion started by: Subsonic66
5 Replies

2. Shell Programming and Scripting

Delete all log files older than 10 day and whose first string of the first line is "MSH" or "<?xml"

Dear Ladies & Gents, I have a requirement to delete all the log files in /var/log/test directory that are older than 10 days and their first line begin with "MSH" or "<?xml" or "FHS". I've put together the following BASH script, but it's erroring out: for filename in $(find /var/log/test... (2 Replies)
Discussion started by: Hiroshi
2 Replies

3. Shell Programming and Scripting

Move a line containg "char" above line containing "xchar"

Okay, so I have a rather large text file and will have to process many more and this will save me hours of work. I'm not very good at scripting, so bear with me please. Working on Linux RHEL I've been able to filter and edit and clean up using sed, but I have a problem with moving lines. ... (9 Replies)
Discussion started by: rex007can
9 Replies

4. Shell Programming and Scripting

Find "*.c" and "Makefile" and then delete them with one line

find "*.c" and "Makefile" and then delete them with one line (3 Replies)
Discussion started by: yanglei_fage
3 Replies

5. Shell Programming and Scripting

Find lines with "A" then change "E" to "X" same line

I have a bunch of random character lines like ABCEDFG. I want to find all lines with "A" and then change any "E" to "X" in the same line. ALL lines with "A" will have an "X" somewhere in it. I have tried sed awk and vi editor. I get close, not quite there. I know someone has already solved this... (10 Replies)
Discussion started by: nightwatchrenba
10 Replies

6. Shell Programming and Scripting

AWK for multiple line records RS="^" FS="#"

I have to pull multiple line records with ^ as the record separator(RS)... # should be my field separator (FS)... Sample record is: ^-60#ORA-00060: deadlock detected while waiting for resource ORA-00001: unique constraint (SARADM.TCKNUM_PK) violated#PROC:AVAILABLE_FOR_GETNXTTIC#02/27/2012... (7 Replies)
Discussion started by: Vidhyaprakash
7 Replies

7. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

8. Shell Programming and Scripting

How to remove "New line characters" and "spaces" at a time

Dear friends, following is the output of a script from which I want to remove spaces and new-line characters. Example:- Line1 abcdefghijklmnopqrstuvwxyz Line2 mnopqrstuvwxyzabcdefghijkl Line3 opqrstuvwxyzabcdefdefg Here in above example, at every starting line there is a “tab” &... (4 Replies)
Discussion started by: anushree.a
4 Replies

9. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies

10. Shell Programming and Scripting

how to make ABC into "ABC" ina file

suppose u have a file ABC CDF ADF FDG HAA AHH AHA so output shud be like "ABC" "CDF" "ADF" FDG " "HAA" "AHH" "AHA" (8 Replies)
Discussion started by: cdfd123
8 Replies
Login or Register to Ask a Question