Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Need to print the expansion of the found string (the expansion is beween two delimiters '-' , '||'

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 10-28-2009
Balaji PK Balaji PK is offline
Registered User
 
Join Date: Oct 2009
Last Activity: 2 November 2009, 7:30 AM EST
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Need to print the expansion of the found string (the expansion is beween two delimiters '-' , '||'

Hi ,
could anyone help me out with this problem.
sample.txt has this content :
u001- this is used for project1 ||
u002- this is used for p2|| not to be printed
u003- this is used
for project3 ||
u004- this is
used for p4 ||
u005- this is used for project5 ||
u006- this is used for p6 ||

I need a shell script , for which the input will be 'u001' or 'u00X'...
and i need to print the words between - and || of that particular string.

I am able to print the word if all the words are in the same line..

a=`grep -iw $1 sample.txt | awk -F"-" '{print $2}' | awk -F"||" '{print $1}'`
echo $a

this holds good for u001,u002,u005,u006..

can anyone help me with the code ...if my input is u003,u004

Linux
Sponsored Links
    #2  
Old Unix and Linux 10-28-2009
Franklin52's Unix or Linux Image
Franklin52 Franklin52 is offline
Registered User
 
Join Date: Feb 2007
Last Activity: 14 October 2017, 3:52 AM EDT
Location: The Netherlands
Posts: 7,746
Thanks: 139
Thanked 558 Times in 520 Posts
Is this homework? This is a similar question to:

https://www.unix.com/shell-programmin...-new-line.html
Sponsored Links
    #3  
Old Unix and Linux 10-28-2009
ahmad.diab's Unix or Linux Image
ahmad.diab ahmad.diab is offline
Registered User
 
Join Date: May 2008
Last Activity: 7 August 2017, 4:42 AM EDT
Location: Amman, Jordan
Posts: 645
Thanks: 2
Thanked 19 Times in 19 Posts
code



Code:
nawk '
/^u00[34]/ {c=2;printf "%s ",$0} ; /^u00[^34]/
!--c
' sample.txt

BR
    #4  
Old Unix and Linux 10-28-2009
panyam panyam is offline Forum Advisor  
Registered User
 
Join Date: Sep 2008
Last Activity: 13 April 2017, 4:34 AM EDT
Posts: 1,160
Thanks: 20
Thanked 105 Times in 100 Posts
Try something like this :


Code:
awk -v va="u003" '$0 ~ va,/\|\|/ {print }' file.txt

To get the text and then your approach to get the text b/w "-" and "||".
Sponsored Links
    #5  
Old Unix and Linux 10-28-2009
Balaji PK Balaji PK is offline
Registered User
 
Join Date: Oct 2009
Last Activity: 2 November 2009, 7:30 AM EST
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
this is printing everythin in the sample.txt.. I want it like if my input is u003 , the out put should be" this is used for project3 "...

pls help it.

---------- Post updated at 05:06 AM ---------- Previous update was at 05:05 AM ----------

awk: syntax error near line 1
awk: bailing out near line 1


this is the error i am getting.
Sponsored Links
    #6  
Old Unix and Linux 10-28-2009
Franklin52's Unix or Linux Image
Franklin52 Franklin52 is offline
Registered User
 
Join Date: Feb 2007
Last Activity: 14 October 2017, 3:52 AM EDT
Location: The Netherlands
Posts: 7,746
Thanks: 139
Thanked 558 Times in 520 Posts
Balaji PK,

Is this home work?
Sponsored Links
    #7  
Old Unix and Linux 10-28-2009
panyam panyam is offline Forum Advisor  
Registered User
 
Join Date: Sep 2008
Last Activity: 13 April 2017, 4:34 AM EDT
Posts: 1,160
Thanks: 20
Thanked 105 Times in 100 Posts
Quote:
Originally Posted by Balaji PK View Post
this is printing everythin in the sample.txt.. I want it like if my input is u003 , the out put should be" this is used for project3 "...

pls help it.

---------- Post updated at 05:06 AM ---------- Previous update was at 05:05 AM ----------

awk: syntax error near line 1
awk: bailing out near line 1


this is the error i am getting.
this is what i am getting :


Code:
TEST/TES>awk -v va="u003" '$0 ~ va,/\|\|/ {print }' file.txt
u003- this is used
for project3 ||

Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
sudo and expansion chebarbudo UNIX for Dummies Questions & Answers 4 01-22-2009 03:32 AM
~ expansion in printf Rledley Shell Programming and Scripting 2 11-15-2008 05:56 PM
Pattern expansion problem njoshi Shell Programming and Scripting 5 04-24-2007 12:28 PM
AIX Expansion CD almuwatta AIX 0 06-23-2005 05:07 PM
ksh on HP-UX -- variable expansion dangral UNIX for Dummies Questions & Answers 4 02-08-2005 11:35 AM



All times are GMT -4. The time now is 12:53 AM.