LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   help with script (https://www.linuxquestions.org/questions/linux-newbie-8/help-with-script-4175440533/)

NickPats 12-08-2012 12:16 AM

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!

shivaa 12-08-2012 12:35 AM

Save this data in a file named file1 and try:
Code:

egrep "abc bas|jhab kam" file1

NickPats 12-08-2012 12:38 AM

I know that one. but the data that I gave has abc bas & jhab kam. but that could be anything.

soupmagnet 12-08-2012 01:04 AM

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

shivaa 12-08-2012 01:30 AM

Quote:

Originally Posted by NickPats (Post 4844989)
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?

druuna 12-08-2012 02:38 AM

Quote:

Originally Posted by NickPats (Post 4844986)
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

NickPats 12-08-2012 08:15 AM

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

druuna 12-08-2012 08:40 AM

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.

NickPats 12-08-2012 08:56 AM

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.

shivaa 12-08-2012 12:52 PM

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.

NickPats 12-08-2012 05:30 PM

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

shivaa 12-08-2012 10:12 PM

Quote:

Originally Posted by NickPats (Post 4845409)
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.

TB0ne 12-10-2012 08:16 AM

Quote:

Originally Posted by NickPats (Post 4845409)
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???

grail 12-10-2012 11:36 AM

Please explain the difference between this question and the one you posted earlier here?

NickPats 12-10-2012 02:47 PM

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.


All times are GMT -5. The time now is 10:32 AM.