LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-08-2012, 12:16 AM   #1
NickPats
Member
 
Registered: Oct 2012
Posts: 43

Rep: Reputation: Disabled
help with script


abc bas
110 jha
-a
-b
-c

jhab kam
648 jad
-d
-e
-f

if i have this type of data and i just wanna print out 'abc bas' and 'jhab kam'. can someone help!
 
Old 12-08-2012, 12:35 AM   #2
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Save this data in a file named file1 and try:
Code:
egrep "abc bas|jhab kam" file1
 
Old 12-08-2012, 12:38 AM   #3
NickPats
Member
 
Registered: Oct 2012
Posts: 43

Original Poster
Rep: Reputation: Disabled
I know that one. but the data that I gave has abc bas & jhab kam. but that could be anything.
 
Old 12-08-2012, 01:04 AM   #4
soupmagnet
LQ Newbie
 
Registered: Sep 2012
Posts: 29

Rep: Reputation: Disabled
If your data maintains reletively the same format (i.e. 5 lines and a space in between) you can use 'sed' to print every Nth line:

http://linuxcommando.blogspot.com/20...every-nth.html
 
Old 12-08-2012, 01:30 AM   #5
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Quote:
Originally Posted by NickPats View Post
I know that one. but the data that I gave has abc bas & jhab kam. but that could be anything.
If you're using same keywords to search, then you can use same search criteria for any file, as:
Code:
egrep "<string1>|<string2>|<string3>..." file1
Or explain what exactly your requirement is?

Last edited by shivaa; 12-08-2012 at 01:35 AM.
 
Old 12-08-2012, 02:38 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Quote:
Originally Posted by NickPats View Post
abc bas
110 jha
-a
-b
-c

jhab kam
648 jad
-d
-e
-f

if i have this type of data and i just wanna print out 'abc bas' and 'jhab kam'. can someone help!
You can re-phrase your requirement to: print all the lines that do not start with a number, a dash or are empty.

Try this:
Code:
sed -n '/^[^0-9\-]/p' infile
EDIT: If the lines wanted always start with an alphanumerical character you can also do this (more elegant):
Code:
sed -n '/^[[:alpha:]]/p' infile

Last edited by druuna; 12-08-2012 at 02:52 AM. Reason: More elegant solution.
 
Old 12-08-2012, 08:15 AM   #7
NickPats
Member
 
Registered: Oct 2012
Posts: 43

Original Poster
Rep: Reputation: Disabled
yeah thanks. i already got that one! but how can i number up the lines that i filtered out from this. i sorted them alphabetically. so now i want

1. abc bas
2. jhab kam

so that if i ask user to choose a number it will print out the info under that. like choosing 1 it will print out
-a
-b
-c

Last edited by NickPats; 12-08-2012 at 08:17 AM.
 
Old 12-08-2012, 08:40 AM   #8
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Can you tell us what all this is used for? It seems that your questions in post #1 and post #7 are related and part of a larger problem.

It would make it easier for us to understand the problem if you tell us more.

- What are you trying to accomplish,
- What is the input and how is it used,
- Is there any user input,
- Any expected output,
- .....

BTW: Please put your script/data inside [code] ... [/code] tags it preserves all spacing. If you don't know how: LQ - BB Code List.
 
Old 12-08-2012, 08:56 AM   #9
NickPats
Member
 
Registered: Oct 2012
Posts: 43

Original Poster
Rep: Reputation: Disabled
yeah i have a database as i gave example above. i am trying to listing all that info as user want. so at first it will print out those titles and ask user if he/she wants in detail about that by entering number as i said.
if he/she enters 1 then it should print all info under it
-a
-b
-c

i am stuck here.
 
Old 12-08-2012, 12:52 PM   #10
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
It seems that you want something like, if user enters 1, then it consider "abc bas" and print the info. that is just below this string (i.e. 4 lines below "abc bas"), and same if user enters 2, it considers "jhab kam"... and so on.

It is ok for a small file. But for a large database, you will need to assign a unique number to each user!! So, will your script user be able to remember number associated with the username entry that he/she's going to serach (I am assuming huge number of username entries in database)? I don't think so.

Instead of using numbers, you should better direct take username of the user whose database entries he/she's seaching, as input from him/her. Following may be helpful:
Code:
#!/bin/bash
echo "Enter username to be searched: "; read username
awk -v var=$(awk '$0 ~ /$username/ {print ((NR))}' file1) 'NR>var && NR<var+4; END{print}' file1
Note: I am assuming that your database file file1 has all entries in same pattern, like:
Code:
<username>
<some_number> <some_string>
-<something>
-<something>
-<something>
I am sorry if I have interpreted your requirement wrongly, but this is what it sounds to me. If it's not what you want, then better explain your requirement in detail with sample database file and sample output that your want.

Last edited by shivaa; 12-08-2012 at 01:09 PM. Reason: Typo
 
Old 12-08-2012, 05:30 PM   #11
NickPats
Member
 
Registered: Oct 2012
Posts: 43

Original Poster
Rep: Reputation: Disabled
yeah that can work but the question that i have it says use numbers. its like user to choose a username by entering the number, or to quit by entering a q
 
Old 12-08-2012, 10:12 PM   #12
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Quote:
Originally Posted by NickPats View Post
yeah that can work but the question that i have it says use numbers. its like user to choose a username by entering the number, or to quit by entering a q
What's correlation between username & numbers? Could you explain what's need of assign a number first and then search using that number? May be people can then better help you.
I feel that you're confused!! So as I said above, if pb has not been solved yet, then clearly mention everything, including sample db file, sample output you want, and your purpose of using numbers instead of usernames.
Also pls use correct grammer & punctuation in your post, so it can be easily understandable.

Last edited by shivaa; 12-08-2012 at 10:14 PM. Reason: Line added
 
Old 12-10-2012, 08:16 AM   #13
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,634

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by NickPats View Post
yeah that can work but the question that i have it says use numbers. its like user to choose a username by entering the number, or to quit by entering a q
Sounds very much like a homework question. Can you post what you've written/tried so far???
 
Old 12-10-2012, 11:36 AM   #14
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Please explain the difference between this question and the one you posted earlier here?
 
Old 12-10-2012, 02:47 PM   #15
NickPats
Member
 
Registered: Oct 2012
Posts: 43

Original Poster
Rep: Reputation: Disabled
i filtered usernames out but now i wanna ask user about who's info he/she wanna see.
like ,
username1
username2

but dont know how can i key those names to numbers. so if username enters 1 it wil give info on 1.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell script/Perl Script to remove the string until it finds special character '_' pooppp Programming 10 07-17-2012 09:36 AM
Shell script/Perl Script to remove the string until it finds special character '_' pooppp Programming 1 07-13-2012 01:03 AM
Shell script, Perl script, command or utility to convert Binary to text Perseus Programming 26 07-12-2012 06:00 AM
[SOLVED] bash and xterm: how make apps started by and for a script persist when script terminates porphyry5 Linux - General 4 06-15-2011 01:27 PM
How to get full path to script file inside script itself? And in case of sym links? maggus Linux - Newbie 3 05-28-2009 08:40 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:50 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration