searching a text string for n'th :


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting searching a text string for n'th :
# 1  
Old 08-25-2010
searching a text string for n'th :

hello, i'm a novice on bsh scripting so thanks for any help here
basically i have a shell var $x that looks like this
Code:
> echo $x
nabc1234:!:73394:17155:Gary Mason:/home/garym:/bin/ksh

and i'm trying to keep the first 8 characters and the text from the 4th : to the 5th :

i've been trying to use the index function, but can figure out how to tell it the nth : to go to
Code:
echo `expr index "$x" $":"`   # returns 8

i know there are many ways to skin this cat.
but if you post a solution, please give me a little explaination/comments
thanks

Last edited by Scott; 08-25-2010 at 11:54 AM.. Reason: Please use code tags
# 2  
Old 08-25-2010
Code:
echo $x | cut -d: -f1,5

To know how this command works, consult "man cut"
This User Gave Thanks to bartus11 For This Post:
# 3  
Old 08-25-2010
Code:
y=${x%:*:*}
echo "${y%%:*} ${y##*:}"

# 4  
Old 08-25-2010
ok yes the cut works

here's my command string, the problem is when i get multiple lines from the passwd file. this only returns the first line's info, the result of the grep seems to put just one space between the lines and the cut thinks that it's looking at one line not 2. i've looked and man grep and man cut to see if there are any switches I should turn on and nothing has worked thus far.
any suggestions to get the second line to echo?

Code:
echo \\n \\r `cat /etc/passwd | grep -E Sean` | cut -d: -f1,5


Last edited by Scott; 08-25-2010 at 09:09 PM.. Reason: Code tags, please...
# 5  
Old 08-25-2010
When you execute a command in back quotes or $() the output is stripped of new lines (by the shell). You don't need to cat the file, grep will read it when given the file name on the command line:

Code:
grep -E Sean /etc/passwd | cut -d: -f1,5


Last edited by agama; 08-25-2010 at 07:56 PM.. Reason: clarification
This User Gave Thanks to agama For This Post:
# 6  
Old 08-25-2010
good point...but

I've got this as a function in my .profile, and with out the ` ticks the grep doesn't invoke.
Double quotes don't help either
thanks

Code:
nbk ()
{
echo \\n \\r grep -E $1 /etc/passwd | cut -d: -f1,5
}
 
 
tx8apbci210:/home/nas/nbk1hhk> nbk Sean
 
 grep -E Sean /etc/passwd
tx8apbci210:/home/nas/nbk1hhk>


Last edited by Scott; 08-25-2010 at 09:10 PM.. Reason: Please use code tags
# 7  
Old 08-25-2010
The grep won't fire because it's in the echo which isn't needed. Try this:

Code:
nbk ()
{
grep -E $1 /etc/passwd | cut -d: -f1,5
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need comand or script for append text after searching for the desired string

Hi all, i have a generated report in unix in the following command like input.txt 47.85,10 0124,42.35,8 0125,3.5,2 the input file format is fixed I need the my output file with append text as below output.txt 0124 amount:42.35 0125 amount:3.5 0124 count : 8 0125... (34 Replies)
Discussion started by: hemanthsaikumar
34 Replies

2. Shell Programming and Scripting

Searching a String

Hi everyone ! suppose i'm searching for a specific string in a file so it is very easy, i use the following command grep 'keyword' file_name but how to search a word which is repeated maximum number of times in a file, for example in the following text i have to search a word which is... (12 Replies)
Discussion started by: ourned
12 Replies

3. Shell Programming and Scripting

searching a file with a specified text without using conventional file searching commands

without using conventional file searching commands like find etc, is it possible to locate a file if i just know that the file that i'm searching for contains a particular text like "Hello world" or something? (5 Replies)
Discussion started by: arindamlive
5 Replies

4. Shell Programming and Scripting

searching the required string and appending string to it.

Hi all, I have some data in the form of adc|nvhs|nahssn|njadnk|nkfds in the above data i need to write a script so thet it will append "|||" to the third occurnace in the string ..... the outout should look like adc|nvhs|nahssn||||njadnk|nkfds Thanks, Firestar. (6 Replies)
Discussion started by: firestar
6 Replies

5. Shell Programming and Scripting

Searching for a particular string and modifying text within block of data

Hi Forum. Is there a quick way to do the following search/replace within a block of data? I tried to google the solution but didn't really know what to look for. I have the following text file (I want to search for a particular string "s_m_f_acct_txn_daily_a1" and replace the... (5 Replies)
Discussion started by: pchang
5 Replies

6. Shell Programming and Scripting

String searching and output to a file in a formatted text

Hi, I'm very new to UNIX scripting and find quite difficult to understand simple UNIX syntax. Really appreciat if somebody could help me to give simple codes for my below problems:- 1) I need to search for a string "TTOH 8031950001" in a files which filename will be "*host*'. For example, the... (3 Replies)
Discussion started by: cuji
3 Replies

7. Shell Programming and Scripting

Searching a string in a file

Hi, I am new to unix shell scripting. I have a requirement. Could anyone help me writing the script for the same? Here goes the requirement: I have a config file let's say temp.config. Here is the data in the config file temp.config : ------------- name=victor age=42 state=texas... (5 Replies)
Discussion started by: badrimohanty
5 Replies

8. Shell Programming and Scripting

PERL: Searching for a string in a text file problem

Looking for a bit of help. I need to search for a string of words, but unfortunately these words are located on separate lines. for example the text output is: United Chanmpions Ronaldo Liverpool Losers Torres and my script code is print("DEBUG - checking file message"); while... (15 Replies)
Discussion started by: meevagh
15 Replies

9. Shell Programming and Scripting

Extracting a string from one file and searching the same string in other files

Hi, Need to extract a string from one file and search the same in other files. Ex: I have file1 of hundred lines with no delimiters not even space. I have 3 more files. I should get 1 to 10 characters say substring from each line of file1 and search that string in rest of the files and get... (1 Reply)
Discussion started by: mohancrr
1 Replies

10. UNIX for Dummies Questions & Answers

Hi! Searching for a text string in UNIX

Hi! I'm new here and glad to meet everyone! I've been wrestling with a problem lately however! How do I recursively (recursive means to keep going through the subdirectories until no more are there) search a bunch of textfiles in a long directory structure for a specific string.. but only... (1 Reply)
Discussion started by: skwadim
1 Replies
Login or Register to Ask a Question