sed - write remaining input and quit?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed - write remaining input and quit?
# 1  
Old 08-22-2010
sed - write remaining input and quit?

I'm an infrequent user of sed. I searched this and other forums and read quite a bit of the O'Reilly book for an answer without luck.

In my script I want sed to write the remainder of the input file and quit if it matches a pattern.

e.g
/Google/

Does anyone have an answer?

thank.
# 2  
Old 08-22-2010
I'm not sure if I understand you correctly.. You want to print contents of a file, until some pattern is matched, and then quit? If so this will do:
Code:
sed '/Google/q' file

# 3  
Old 08-22-2010
Hi
If I had understood you correctly:
Code:
# cat f1
Welcome to unix forum
Always if possible,
try to provide sample input
and output. It helps us to provide
solutions faster.
Google is my
dream company by the way.
# sed -n '/Google/q;p' f1
Welcome to unix forum
Always if possible,
try to provide sample input
and output. It helps us to provide
solutions faster.
#

Guru.
# 4  
Old 08-22-2010
Thanks for the quick replies.

Sorry for not being clear enough. Hopefully the following helps.

The quit command only provides half of what I want.
/Processed/q

I want the sed script to print the input until it matches the pattern then print the matched input line and the remainder of the input file THEN quit.
Kind of like:
/Processed/wq

There are other editing commands in the sed script which should be executed if matched but if the /Processed/ pattern is matched I want the all of the input printed to the output.

I'm using the /Processed/ pattern to indicate the file has previously been edited. The 'Processed' line gets read in along with some other text the first time the input file is processed. /pattern/r moretext.txt

Because the sed script could be run multiple times over both processed and unprocessed files I don't want the previously processed files to have multiple additions of the moretext.txt file.

thanks.
# 5  
Old 08-22-2010
Quote:
Originally Posted by lyledp
I want the sed script to print the input until it matches the pattern then print the matched input line and the remainder of the input file THEN quit.
That sounds like you want to print out every line before the pattern, the line that matches the pattern, and every line after the pattern. In short, `cat file`.

Why don't you post the sed script you're using, some sample input, and the desired output?

Regards,
Alister

---------- Post updated at 05:08 PM ---------- Previous update was at 04:49 PM ----------

Upon re-reading your post, I think what you are trying to do is print input lines in a loop so that other sed commands are not executed. If so, perhaps this may be helpful:

Code:
$ jot 10
1
2
3
4
5
6
7
8
9
10

$ jot 10 | sed -f skip.sed 
1: s command executed
2: s command executed
3: s command executed
4: s command executed
5
6
7
8
9
10

$ cat skip.sed 
/5/ {
    :pr
    n
    b pr
}

s/.*/&: s command executed/

# 6  
Old 08-22-2010
Alister, your skip.sed does exactly what I need. thanks.

Thanks to everyone else for their time.

ps: I still think 'wq' might have been a natural command to have in sed.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read input write multply output with creteria

Hi All Please Help Read input write multply output with creteria Exemple i have file abc 111 444 abc 111 444 def 111 444 def111 444 bbb 111 444 bbb 111 444 i would need write 3 files pos 1-3 is the Criteria output would be file1 contains abc file2 def file3 bbb ... (3 Replies)
Discussion started by: tonyk334
3 Replies

2. Shell Programming and Scripting

ksh loop to read input until QUIT

Hi I'm looking to write a simple ksh loop reading user input (and write it to a file) until the user enters QUIT at which point I want it to continue. Does anyone have an example of this type of loop? Any help much appreciated Cheers (2 Replies)
Discussion started by: Grueben
2 Replies

3. Shell Programming and Scripting

Read input files and merge them in given order and write them to input one param or one file

Dear Friends, I am looking for a shell script to merge input files into one file .. here is my idea: 1st paramter would be outfile file (all input files content) read all input files and merge them to input param 1 ex: if I pass 6 file names to the script then 1st file name as output file... (4 Replies)
Discussion started by: hyd1234
4 Replies

4. Shell Programming and Scripting

Write a shell program with input

Hi, Here is my question: I want a shell script which I name as 'del', and can be used as del(string). when run del(string), it will delete several directories at different locations in my system,like: rm -fr /lustre/fs/scratch/user/$string rm -fr /home/user/$string rm -fr... (4 Replies)
Discussion started by: 1988PF
4 Replies

5. UNIX and Linux Applications

Input a variable and write to a file using awk

Hi I am trying to edit a csv file. Bacically I need to input a search variable and the value that must be changed in one of the fields corresponding to that searched variable. My csv file looks like so: 1,1A,5 1,1B,2 1,1C,3 2,2A,7 2,2B,4 2,2C,0 3,3A,1 3,3B,6 3,3C,4 I want to... (4 Replies)
Discussion started by: ladyAnne
4 Replies

6. Shell Programming and Scripting

"Write" - how to quit by press enter

I need to make Bash script. It has one parameter - user ID. If this user is online you can write him a message with "write" program, but only one line. After pressing ENTER "write" program should quit. Normally when you run "write" you can write next line after pressing ENTER and you can quit... (0 Replies)
Discussion started by: Eriknem
0 Replies

7. Shell Programming and Scripting

perl how to exit a while loop and quit reading the input file

I am reading a file using While loop while <FILE> { $_ = <FILE>; process data... } I would like to quit reading the file once I encounter a String pattern. How do i do it. is it if (/SUMMARY/) { last; } I am having problems with uninitialized value in pattern... (1 Reply)
Discussion started by: subhap
1 Replies

8. Shell Programming and Scripting

Write a new file from 2 files as input to the script

Hi- I am hoping someone can give me some pointers to get me started. I have a file which contains some dn's .e.g file 1 cn=bob,cn=user,dc=com cn=kev,cn=user,dc=com cn=john,cn=user,dc=com I have a second file e.g. file.template which looks something like :- dn: <dn> objectclass:... (5 Replies)
Discussion started by: sniper57
5 Replies

9. UNIX for Dummies Questions & Answers

Vi, write something then try to save & quit.

If I'm in Vi, write something then try to save & quit. :wq I get: "myvifile" "myvifile" E212: Can't open file for writing Press ENTER or type command to continue It won't let me save... Is it because other users on the network have access to the file also? Or I don't have permission to save? Thanks... (5 Replies)
Discussion started by: JudoMan
5 Replies
Login or Register to Ask a Question