[Show all top banners]

Saajha
Replies to this thread:

More by Saajha
What people are reading
Subscribers
Subscribers
[Total Subscribers 1]

Slackdemic
:: Subscribe
Back to: Kurakani General Refresh page to view new replies
 SHELL SCRIPT HELP...
[VIEWED 4459 TIMES]
SAVE! for ease of future access.
Posted on 06-06-07 4:41 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

A quick question to Shell scripting enthusiasts (might be too trivial for the pros)

Name of the file: "filename.txt"

If I am to generate a list of lines that include the string "I am the KEY"; I'll run the following:

cat filename.txt | grep "I am the KEY: List Me" | cut -c01-30

... to get upto the 30th character.

Now, if I want to see the line above it, the line below it, and the line even below it ( that optionally exists), which could potentially extend longer than one line-- as in the following scenario:

"I want this listed"
"I am the KEY: List Me"
"I want this listed"
"If I exist, list me too; BUT make sure you list all of me even if I extend beyond one line"

How would that be accomplished?

(Note: it's a windows' notepad file running on cygwin)

Anyone?

~@~
 
Posted on 06-06-07 5:42 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

you don't even need cat here...
simply do like this grep -A m -B n "search pattern" filename

where m is the no. of lines after it and n is the no. of lines before it..

hope this helps ...
 
Posted on 06-06-07 9:14 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

That helps, thank you - but here's where the glitch remains:

'n' is fine, because I know for sure there's only one line above 'n', and that's got to come up!
But 'm' needs branching --
Now, 'm' could be 1, OR it could be 2.If I simply plug in 2 to m; it works fine, given that "If I exist, list me too..." line is present. But since this is optional, some other string occupies that space during its absence. So, the only way I can think of is to Pipe one more command set with an additional grep that would give me that particular line whenever it is present, and obey the 'm' = 2 ruleset, otherwise completely ignore it. Does that make sense? :|

~@~
 
Posted on 06-06-07 9:16 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

Please read "above n" as "above the line" :P
~@~
 
Posted on 06-06-07 9:32 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

i am a bit confused with your requirement...

ok you mean if the line "If I exist, list me too; BUT make sure you list all of me even if I extend beyond one line"

exists, then you wish m to be 2 right; else 1 right ??

but here i have a question, that line in real case won't be always the same....in that case what factor do you wish to use to determine the value of m to be ??

let me know your requirements a bit more clearly... will try my best...
 
Posted on 06-07-07 7:10 AM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

It sounds like you are asking me what if the line extends beyond 1, referring to the second condition of the statement, "If I exist, list me too; BUT make sure you list all of me even if I extend beyond one line". That actually was my question initially, but then I figured I would be able to pass that through by hiking up the value -c01-30 to say -c01-150, assuming that the line doesn't end with a 'space'; that way it continues reading the line below that as well, whenever it has to.(Not a great way to get around, but I can live with that for now !) Hope that answers your question !

HOWEVER, problem still persists :( --- I am yet to feed in a command to fulfil "If I exist, list me too".

Now grep is already fed with one condition. I could pipe, say a " grep -v", if I knew the exception, which is not the case here. In other words, is there a way to pipe multiple greps (without switches) with different "search patterns"?

Looking Forward ...

~@~
 
Posted on 06-07-07 8:24 AM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

yeso garum nah bro...Nepali mah problem k ho lekhum haagi...will try to find the solution to my best...cause i enjoy to playing with these...

chaaatah nepali mah problem lekhum from beginning...will find a solution :)

shell script leh kaam nagare aaru jindabaaad garum lah nih...
 
Posted on 06-07-07 8:30 AM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

Dude You Rock --
I will do that in Nepali, but please give me some time for that -- I need to do some Kung-Foo to well present all that in Nepali ! :P
Thanks for your effort.

~@~
 
Posted on 06-07-07 8:38 AM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

no worries...will try my best...

you are most welcome...
 
Posted on 06-07-07 11:32 AM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

I am looking for a job that might need shell scripting. Rookie.
Couldnt understand a word what u guys were talking about.
Aba maro ke huncha??
hehehe
 
Posted on 06-07-07 11:52 AM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

Ok saajha,

If the sentence you are trying to grep is always going to be a second line, then the total number of lines in you file after the "I am the KEY: List Me" is going to be total number of lines in the file minus 2. Try creating a simple script for this as below:

#cat myscript
#!/bin/ksh
numb=`cat /path/to/filename.txt | wc -l`
after=`expr $numb - 2`
grep -$after m -B n "search pattern" filename

Hope this helps!

Cheers,
haami
 
Posted on 06-07-07 6:37 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

That's an interesting approach haami,
So you defined 'numb' = total number of lines in the file
'after' = total number -2
Then you grepped that 'after' to get the result, correct?

Hmm, that's agreeable -- but the situation is different here.

It's a huge text file -- close to thousand pages long ..so the file doesn't end immediately. Also, the string repeats after random intervals; which means there is no way of specifying its occurence in terms of line number.

Once again, following are the lines (let me try explaining in Nepali as per sangfroid):

"I want this listed"
"I am the KEY: List Me"
"I want this listed"

yo tin-wata lines chahi hunai parchha hai ---
Aba 4th line optional chha.
5th line we are not concerned with -- tara 4th line nai extend bhayera 5th, 6th pugna sakchha..

So, "I am the KEY: List Me" chahi every random interval ma auchha..! Aba m=1 ta thik chha, n = 1 pani thik chha, as long as there is no line 4, line 5 or more..tara what if there is line 4 +. ..!!
The Good News is : Line 4 ko string pani given chha "If I exist, list me too; ..."
The Bad News is : Grep is already looking for the string "I am the KEY: List Me"
The Proposal is: Dui wata string lai nai search pattern ma PIPE garera halna sakyo bhane ta problem solve hunuparchha hola ...since it looks for both character strings and gives out only the lines corresponding to those two.
The Enhanced Proposal is: Tyo Line 4 extend bhayo bhane 'cut c-01-150' le 150 characters search garchha, and that would include the other line too. Tyo line ma 150 characters chhaina bhane, it would simply give us the ones that are present. Tara pani, there must be a better way to do that.

Solution: :( ?

Thanks Guys !!
~@~

PS: Sorry about Nep-English confusion --
 
Posted on 06-08-07 10:28 AM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

Hey saajha,

Have you tried looking at 'egrep' options instead of just 'grep'? egrep gives you more search pattern options.

haami..
 
Posted on 06-08-07 11:01 AM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

haami,
Believe it or not, I was just speaking with one of my coworkers regarding this, and THAT'S EXACTLY WHAT HE SAID -- "Let's Try egrep". So I ran a quick 'man' on egrep, read through it, and fired up sajha.com to add that proposal to the list -- and there you are, speaking the same word --
Way to go !!! I'm gonna hit the bash with egrep and see if it brings some sweet fruit !

Thanks,
~@~
 
Posted on 06-08-07 12:01 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

Hey Saajha,

I wish I recommended the option little earlier, so that you didn't have to scramble for this issue.

Let us know how it goes.

haami..
 
Posted on 06-08-07 3:50 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

One way to search multiple strings with grep is by using a list of those strings in separate text file.

cat > pattern.txt
string 1
string 2

Now you can use it as " grep -f pattern.txt input_file.txt | ..."

Also, in some version of wc it will print the input file name after the line count.
You may do:

wc -l input_file.txt | awk '{ print $1 }'
 
Posted on 06-08-07 4:14 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

.

Try AWK instead !

# awk -f test.awk filename.txt

test.awk
/I am the KEY: List Me/{print x};{x=$0}
/I am the KEY: List Me/
/I am the KEY: List Me/{getline;print}

this prints one line above , one line below .

i guess, you can do some loop-checking after the third statement.

If you know the STOP regular expression statement, there is a better mechanism .. too , like :

awk '/I want this listed/, /THE STOP LINE REGEXP;/' filename.txt
 
Posted on 06-08-07 4:17 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

I took an easier approach --
Asked a DB guy to regenerate a report in csv, and simply sorted the lines on Excel -- I tell ya' Microsoft turns you lazy, making things easy !!!

Thanks haami, sangfroid - for all your input; and goodluck to you 'danger'.
I'll certainly play around with egrep more over the weekend though.
Happy Friday !!

~@~
 
Posted on 06-08-07 8:25 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

Wow.. 'awk' was another approach I tried before "migrating" toward the spreadsheet this afternoon. Thanks spitfire and oldmaven for your inputs. Will look into them over the weekend. Glad to know we have some good scripters around.. !! :)

~@~
 


Please Log in! to be able to reply! If you don't have a login, please register here.

YOU CAN ALSO



IN ORDER TO POST!




Within last 200 days
Recommended Popular Threads Controvertial Threads
शीर्षक जे पनि हुन सक्छ।
NRN card pros and cons?
What are your first memories of when Nepal Television Began?
TPS Re-registration
TPS Re-registration case still pending ..
Democrats are so sure Trump will win
Basnet or Basnyat ??
nrn citizenship
Sajha has turned into MAGATs nest
Nas and The Bokas: Coming to a Night Club near you
ChatSansar.com Naya Nepal Chat
डीभी परेन भने खुसि हुनु होस् ! अमेरिकामाधेरै का श्रीमती अर्कैसँग पोइला गएका छन् !
3 most corrupt politicians in the world
अमेरिकामा बस्ने प्राय जस्तो नेपालीहरु सबै मध्यम बर्गीय अथवा माथि (higher than middle class)
if you are in USA illegally and ask for asylum it is legal
Travelling to Nepal - TPS AP- PASSPORT
emergency donation needed
Top 10 Anti-vaxxers Who Got Owned by COVID
आज बाट तिहारको सेल सकियो
निगुरो थाहा छ ??
Nas and The Bokas: Coming to a Night Club near you
Mr. Dipak Gyawali-ji Talk is Cheap. US sends $ 200 million to Nepal every year.
Harvard Nepali Students Association Blame Israel for hamas terrorist attacks
TPS Update : Jajarkot earthquake
NOTE: The opinions here represent the opinions of the individual posters, and not of Sajha.com. It is not possible for sajha.com to monitor all the postings, since sajha.com merely seeks to provide a cyber location for discussing ideas and concerns related to Nepal and the Nepalis. Please send an email to admin@sajha.com using a valid email address if you want any posting to be considered for deletion. Your request will be handled on a one to one basis. Sajha.com is a service please don't abuse it. - Thanks.

Sajha.com Privacy Policy

Like us in Facebook!

↑ Back to Top
free counters