Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

extracting partial string 1

Status
Not open for further replies.

tomte

Technical User
Nov 14, 2002
47
0
0
US
I want to extract all the characters after a certain string is found using fgrep, to the end of the current line.

fgrep 'abcde: ' filename

I want to extract to variable all the characters after the :

Thanks in advance
 
You could do it all in awk

awk '{if (match($0,/abcde:/)) print substr($0,RSTART+RLENGTH+1)}' filename CaKiwi
 
Thank you I will give it a try.
 
How 'bout...

while read LINE
do
myVar=`echo $LINE | sed 's/abcde:\(.*\)/\1/'`
done < filename > results
 
How about

fgrep &quot;abcde:&quot; filename | while read line
do
echo ${line#*:}
done > results

The ${line#*:} will drop everything before and including the first :


 
I have a slightly different question, but it's related to tomte's original question.

I need to do a find on a filename, then extract the path only, not the filename, from the results of the find. So, I would do a find on, say, passwd. The results are /usr/bin/passwd, /etc/passwd, and /etc/security/passwd. I want to get just /usr/bin, /etc, and /etc/security.

Thanks for any help.
 
#!/bin/ksh
a='/a/b/c/d'

echo &quot;a->[${a}] path2a->[${a%/*}]&quot;
vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Thanks to all who helped. I have a working extraction now, but I have an interesting situation. I have extracted the string to a variable NAME
I then want to append some string information to the string.
$NAME echos to the appropriate string ABCD
I am in a ksh script and I am using the syntax of
NAME=$NAMEyz
What I end up with is xyCD
I have tried NAME=$NAME'xy'
I have tried NAME=&quot;$NAMExy&quot;
and any other variation of this and end up with the same results. Any suggestions?
 
Actually, I have found that there is a ^M at the end of my string that is causing this to happen, but I cannot seem to sed out the character, anyone have a idea. I have tried sed 's/.^M//g' but it does not seem to take care of the issue.
 

You could try

${NAME%^M}xy

Note that the ^M should be a &quot;control-M&quot;, not 2 separate characters.
 
A lot of times, if you have an extra ^M at the end of your lines, it's because the file either came from a Windows/MSDOS machine, or was edited by a Windows/MSDOS editor (i.e. Notepad via SAMBA). The end of line character for Windows/MSDOS text files is a CR/LF pair, not just the LF that *nix uses. That extra CR is your ^M at the end of the line.

If this is the case, look for a utility called [tt]dos2unix[/tt]. It's purpose is to scrape those ^Ms off.

Hope this helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top