cut part of line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting cut part of line
# 1  
Old 12-16-2007
cut part of line

Dear Sirs,

I want to cut the IP address from the following text line:

"[1] mgmt.mib-2.bgp.bgpPeerTable.bgpPeerEntry.bgpPeerLastError.163.121.170.20 (OctetString): 0x04 00"

I want to get the (163.121.170.20) only.


Thanks in advance.
# 2  
Old 12-16-2007
in shell:

echo "[1] mgmt.mib-2.bgp.bgpPeerTable.bgpPeerEntry.bgpPeerLastError.163.121.170.20 (OctetString): 0x04 00" | cut -d . -f 7- | cut -d " " -f 1

dunno if you want more than that, sometimes you have to say what you want, rather than thinking about what you think is the good solution for your problem and ending up with a loop of problems Smilie
# 3  
Old 12-16-2007
GNUawk
Code:
# more file
[1] mgmt.mib-2.bgp.bgpPeerTable.bgpPeerEntry.bgpPeerLastError.163.121.170.20 (OctetString): 0x04 00
#awk '{b=gensub(/.*[. ]([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+).*/,"\\1","g",$0);print b}' file
163.121.170.20

# 4  
Old 12-16-2007
Quote:
Originally Posted by zouhair
in shell:

echo "[1] mgmt.mib-2.bgp.bgpPeerTable.bgpPeerEntry.bgpPeerLastError.163.121.170.20 (OctetString): 0x04 00" | cut -d . -f 7- | cut -d " " -f 1

dunno if you want more than that, sometimes you have to say what you want, rather than thinking about what you think is the good solution for your problem and ending up with a loop of problems Smilie
Zouhair,
Thank you very much, It works well.
# 5  
Old 12-16-2007
use sed

Hi,

Code:
echo "[1] mgmt.mib-2.bgp.bgpPeerTable.bgpPeerEntry.bgpPeerLastError.163.121.170.20 (OctetString): 0x04 00" | sed '
s/.*\.\([0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,2\}\).*/\1/'

# 6  
Old 12-17-2007
Quote:
Originally Posted by zouhair
echo "[1] mgmt.mib-2.bgp.bgpPeerTable.bgpPeerEntry.bgpPeerLastError.163.121.170.20 (OctetString): 0x04 00" | cut -d . -f 7- | cut -d " " -f 1
isn't it easier to use "... | cut -d'.' -f7-10"?

bakunin
# 7  
Old 12-17-2007
Quote:
Originally Posted by bakunin
isn't it easier to use "... | cut -d'.' -f7-10"?

bakunin
No, because then you will have the rest of the string:
zouhair@gutzy:~ (.318 MB) $ echo "[1] mgmt.mib-2.bgp.bgpPeerTable.bgpPeerEntry.bgpPeerLastError.163.121.170.20 (OctetString): 0x04 00" | cut -d . -f 7- | cut -d " " -f 1
163.121.170.20
zouhair@gutzy:~ (.318 MB) $ echo "[1] mgmt.mib-2.bgp.bgpPeerTable.bgpPeerEntry.bgpPeerLastError.163.121.170.20 (OctetString): 0x04 00" | cut -d . -f 7-10
163.121.170.20 (OctetString): 0x04 00
zouhair@gutzy:~ (.318 MB) $
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to cut part of a string in reverse?

Hi, how to cut part of a string sing delimiter in reverse input file 1,2,st-pa-tr-01,2,3,4, 2,3,ff-ht-05,6,7,8 how can i obtain strings till st-pa-tr ff-ht i.e cutting the last part og string -01 and -05 Thanks & Regards Nivi edit by bakunin: changed thread title (typo) (3 Replies)
Discussion started by: nivI
3 Replies

2. Shell Programming and Scripting

Reading text file, comparing a value in a line, and placing only part of the line in a variable?

I need some help. I would like to read in a text file. Take a variable such as ROW-D-01, compare it to what's in one line in the text file such as PROD/VM/ROW-D-01 and only input PROD/VM into a variable without the /ROW-D-01. Is this possible? any help is appreciated. (2 Replies)
Discussion started by: xChristopher
2 Replies

3. Shell Programming and Scripting

[Solved] Printing a part of the last line of the specific part of a file

Hi, I have 80 large files, from which I want to get a specific value to run a Bash script. Firstly, I want to get the part of a file which contains this: Name =A xxxxxx yyyyyy zzzzzz aaaaaa bbbbbb Value = 57 This is necessary because in a file there are written more lines which... (6 Replies)
Discussion started by: wenclu
6 Replies

4. Shell Programming and Scripting

cut the some part in filename

Hi All, I have the file & name is "/a/b/c/d/e/xyz.dat" I need "/a/b/c/d/e/" from the above file name. I tryning with echo and awk. But it not come. Please help me in this regard. Thanks & Regards, Dathu (3 Replies)
Discussion started by: pdathu
3 Replies

5. Slackware

How should I cut this line using cut and grep?

not sure how to do it. wan't to delete it using cut and grep ince i would use it in the shell. but how must the command be? grep "64.233.181.103 wwwGoogle.com" /etc/hosts | cut -d the delimeter is just a space. can you help meplease. :D (1 Reply)
Discussion started by: garfish
1 Replies

6. UNIX for Dummies Questions & Answers

cut and print part of a string

I have a file that contains: yahoo.com.23456 web.log.common.us.gov.8675 192.168.1.55.34443 john-doe.about.com.22233 64.222.3.4.120 sunny.ca.4442 how can i remove the strings after the last dot (.) and reprint the file? Thanks. (3 Replies)
Discussion started by: apalex
3 Replies

7. UNIX for Dummies Questions & Answers

How to cut a string in two parts and show the other part

hi everybody.. I have a string like : abcd:efgh xxyy:yyxx ssddf:kjlioi ghtyu:jkksk nhjkk:heuiiue please tell me how i can display only the characters after ":" in the output the output should be : efgh yyxx kjlioi jkksk heuiiue please give quick reply.. its urgent..!! (6 Replies)
Discussion started by: adityamitra
6 Replies

8. UNIX for Dummies Questions & Answers

Help me with cut part 2..

Hi, I have this file name : xxx.77876767575.abc.77887.iiii If to get only the xxx i will need to do this command: i=xxx.77876767575.abc.77887.iiii name=`echo $i |cut -f1 -d "."` How do i get 77876767575.abc.77887.iiii without xxx in front? Please advice. Thanks (7 Replies)
Discussion started by: luna_soleil
7 Replies

9. Shell Programming and Scripting

Need to cut a part of a XML file

I want to be able to search and remove a part of this file. For Example I want to make a search for something like Terminal.app and remove the following from the XML file. <dict> <key>GUID</key> <integer>210535539</integer> <key>tile-data</key> <dict> <key>dock-extra</key>... (9 Replies)
Discussion started by: elbombillo
9 Replies

10. Shell Programming and Scripting

cut a part of the file

This is a part of the output file (x.out, from a chemical software). I need to extract the xyz coordinates with the atom labels from this file.If it's possible i would like to use a bash shell script. I am new to this forum, how can I solve this problem. DIPOLE X Y Z ... (1 Reply)
Discussion started by: kurosaki
1 Replies
Login or Register to Ask a Question