Visit Our UNIX and Linux User Community


select contents between two delimiters (not working if newline in encountered)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting select contents between two delimiters (not working if newline in encountered)
# 1  
Old 11-02-2009
select contents between two delimiters (not working if newline in encountered)

Hi,

I am facing difficulties in selecting the contents between two delimiters when there is a new line occurs..

Eg:
Code:
>more sample.txt
abcd -- this is the first line %
efgh-- this is the 
second line and not 
able to print %
ijkl -- this is the 3rd line %


when i search for abcd and use the below command , i am able to print the desired contents between '--' and '%'.
But the same i am not able to print in the case of 'efgh' . Its only printing the 1st line of that word found.

pls tell me what correction to be included in this code.. or suggest me the code for this .
Code:
grep -w efgh sample.txt | awk -F"-" '{print $2}' | awk -F"||" '{print $1}'



Required output : this is the
second line and not
able to print


thanks in advance.
:-)Smilie

Last edited by Balaji PK; 11-02-2009 at 05:46 AM.. Reason: code tags, please...
# 2  
Old 11-02-2009
code

Code:
 nawk  '1' ORS="  "  file.txt | nawk '{print $4}' FS="--|%"

the above code will print :-

"this is the second line and not able to print" in one line.

BR
# 3  
Old 11-02-2009
try:
Code:
#  sed -n '/efgh/,/%/{s/efgh-- //;s/%//;p;}' sample.txt
this is the
second line and not
able to print

HTH

Last edited by Tytalus; 11-02-2009 at 05:58 AM.. Reason: forgot to remove the %
# 4  
Old 11-02-2009
This is fine.. but i want it generalised. I mean i should be able to give input text to it..

---------- Post updated at 05:10 AM ---------- Previous update was at 05:08 AM ----------

Code:
#sed -n '/efgh/,/%/{s/efgh-- //;s/%//;p;}' sample.txt

This gives the desired output for that 'efgh'
But does not give correct output for 'abcd'..
pls suggest

Code:
sed -n '/abcd/,/%/{s/efgh-- //;s/%//;p;}' sample.txt
abcd -- this is the first line
this is the
second line and not
able to print
[KRYPTON]/appltgb/test/repc/balaji >



---------- Post updated at 05:23 AM ---------- Previous update was at 05:10 AM ----------

can u pls suggest a generalised code for the problem stated above.

Last edited by pludi; 11-02-2009 at 06:16 AM.. Reason: code tags, please...
# 5  
Old 11-02-2009
code

Code:
nawk  '1' ORS="  "  input_file | nawk '{print $2,$4,$6}' OFS="\n"  FS="--|%" > output_file

Code:
more input_file:-

abcd -- this is the first line %
efgh-- this is the 
second line and not 
able to print %
ijkl -- this is the 3rd line %

more output_file:-

this is the first line
this is the  second line and not  able to print
this is the 3rd line

# 6  
Old 11-02-2009
well this is good. But when i change the input file ,,i mean if i insert two more lines..it will not work..pls suggest a code that will help me even if i change the input file. the objecive is to give a input file , a word to search in it. once the word found then print the contents corresponding to the word from '--' to '%' and stop.
Am i clear . ?
# 7  
Old 11-02-2009
Why don't you try something for yourself with all the the solutions given to you?

This is not a helpdesk service, we're not here to do your work for you.

Previous Thread | Next Thread
Test Your Knowledge in Computers #447
Difficulty: Medium
DebianDog is also Puppy Linux.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Select command with variable options having spaces or null contents

Hi, I'm having an issue trying to produce a hierarchical directory menu that has either any directories listed in a specific directory as options or options with spaces in the option content or null content. So the menu function gets passed a base directory, it then lists any .sh scripts in... (6 Replies)
Discussion started by: andyatit
6 Replies

2. UNIX for Dummies Questions & Answers

Looping/Reading file contents not working

Hi, I am doing something basic, but I am missing something. Im trying to read the contents of a file and taking those values and connecting to a database. However, it only connect to one (or reads in) value and then exists. Here is what it looks like: listname.txt db1 db2 db3 Script:... (15 Replies)
Discussion started by: DBnixUser
15 Replies

3. Shell Programming and Scripting

Remove newline character between two delimiters

hi i am having delimited .dat file having content like below. test.dat(5 line of records) ====== PT2~Stag~Pt2 Stag Test. Updated~PT2 S T~Area~~UNCEF R20~~2012-05-24 ~2014-05-24~~ PT2~Stag y~Pt2 Stag Test. Updated~PT2 S T~Area~METR~~~2012-05-24~2014-05-24~~test PT2~Pt2 Stag Test~~PT2 S... (4 Replies)
Discussion started by: sushine11
4 Replies

4. Shell Programming and Scripting

need to grep contents of a file within specific time span. regex i am using is not working

Hi , I am trying to extract contents of a file between specified time stamp. but it does not seem to work. i am trying to extract output of /var/adm/messages between 15:00:00 to 15:23:59 . i have tried two regex the first one seems to kind of work. it displays some output. the second one is... (13 Replies)
Discussion started by: chidori
13 Replies

5. Shell Programming and Scripting

How do i select all contents between two numbers?

I want to select contents between two numbers say 1. and 2. from an output file which has many numbers but only the these two ending with a dot(.) eg 1. 2 . 32. etc I was looking to select with the use of a counter then modify the selected contents and put it in an output file where again the... (3 Replies)
Discussion started by: ausfragen
3 Replies

6. Shell Programming and Scripting

Select the exact matching contents using grep

Hi everyone I've two files.. The contents of file1 are as shown below 4 5 12 13 36 37 45 46 47 The contents of file2 are as shown below 21 hello 13 world (5 Replies)
Discussion started by: abk07
5 Replies

7. Shell Programming and Scripting

string deletion, variable contents, fixed delimiters

Hi, I need to mass delete the following string(s) from my files weight=100, However the '100' is variable e.g Current: ---------------- moretext=bar, weight=100, moreinfo=blah extrastuff=hi, weight=9999, extrainfo=foo Desired: ------------------ moretext=bar, moreinfo=blah... (2 Replies)
Discussion started by: rebelbuttmunch
2 Replies

8. Shell Programming and Scripting

script not working...select utility

#!/usr/bin/bash name="$@" myname=malay #echo $myname select firstname in $name; do if ;then echo $firstname else break fi done invoking with:- ./script.sh one two three four five six seven eight nine malay (6 Replies)
Discussion started by: mobydick
6 Replies

9. UNIX Benchmarks

Encountered error!

I used this on an AIX machine and encountered the following error. $ ls -l total 600 -rwxrwxrwx 1 e26936 dba 1491 Feb 07 1992 MANIFEST -rwxrwxrwx 1 e26936 dba 8148 Apr 05 1992 Makefile -rwxrwxrwx 1 e26936 dba 4852 Sep 06 2003 README -rwxrwxrwx... (0 Replies)
Discussion started by: puspendu
0 Replies

10. Shell Programming and Scripting

Newline character not working for ksh

Newline character "\n" not working for ksh in linux AS 3.0 Command : $echo "Hi\nHi" $Hi\nHi $ Expected output : $echo "Hi\nHi" Hi Hi $ Can some help me on this Thanks in advance Sanish. (11 Replies)
Discussion started by: sanikv
11 Replies

Featured Tech Videos