Visit Our UNIX and Linux User Community


How to separate data with varying amounts of underscores?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to separate data with varying amounts of underscores?
# 1  
Old 10-31-2014
How to separate data with varying amounts of underscores?

All, I've searched and haven't found a solution for my particular issue.

I have a file with lines of data that contain varying amounts of underscores imbedded. But all the strings have a final underscore and an interface name:

dmk_hcn_dalian2.XXXX.XXX.XX.COM_Se0/0/0
cn_beijing_terminalserver.XXX.XX.COM_Se0
aurtrpara_1.XXXXX.XXX.XX.COM_Tu108
aurtrpara_2.XXXX.XXX.XX.COM_Tu202
dmk_hcn_dalian1.XXX.XX.COM_Se0/0/0
buranda_2.XXX.XX.COM_Tu215

I need to cut each line at the last underscore, keeping the interface name as one variable, and the Fully Qualified Domain name as another. The problem is that the domain has varying amount of periods as well.
Does anyone have a recommendation to get the output I need with awk/cut/ or sed?

appreciate any suggestions!
# 2  
Old 10-31-2014
Try
Code:
sed 's/^\(.*\)_\(.*\)/\1 \2/' file

To put the values in variables try this
Code:
sed 's/^\(.*\)_\(.*\)/\1 \2/' file | while read fqdn if
do
echo "fqdn: $fqdn if: $if"
done

Hope this helps.
This User Gave Thanks to junior-helper For This Post:
# 3  
Old 10-31-2014
Yes! this works, and I can simply awk the side I want for the variable.
Thanks so much for the quick reply
# 4  
Old 10-31-2014
You're welcome Smilie

Btw, here is another approach, handling it with bash's built-ins only, just in case...
Code:
while read line
do
 fqdn=${line%_*}
 if=${line##*_}
# do something with $fqdn and $if here
done <file

This User Gave Thanks to junior-helper For This Post:
# 5  
Old 10-31-2014
I verified that the bash built in's work as well. I may use this as its easier to understand how it works. Smilie

Previous Thread | Next Thread
Test Your Knowledge in Computers #273
Difficulty: Easy
In December 1939, Turing solved the essential part of the naval indicator system, which was easier and less complex than the indicator systems used by the other services.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to Dump data into CSV file which is Separate by <tab>?

Dear Team, please help me to solve this problem using Linux command. I want to dump this data into an excel sheet, Suppose I have a string like: ABC PQR XYZ ASD then I expect output as a ABC XYZ PQR ASD (3 Replies)
Discussion started by: Shubham1182
3 Replies

2. Shell Programming and Scripting

How to separate data coming in one column of CSV file?

I am running an ISQL command on Sybase DB and getting output of a query in an CSV file. The issue is that all the data comes in to the same column, i want them to be separated in different columns. SQL_COMMAND=command.sql file=file.txt formatFile=formatFile.txt report=report.csv echo... (1 Reply)
Discussion started by: Sharma331
1 Replies

3. Shell Programming and Scripting

How to separate rows of data into another column?

I have data such as below where the value in second field is the same as that in the row after. 123456,22222,John,0,xyz 234567,22222,John1,1,cde 43212,3333,Jean,3,pip 84324,3333,Abel,2,cat I'd like to rearrange the output like below to put such records beside each other and separated with... (5 Replies)
Discussion started by: james2009
5 Replies

4. Shell Programming and Scripting

Divide data with specific column values into separate files

hello! i need a little help from you :) ... i need to split a file into separate files depending on two conditions using scripting. The file has no delimiters. The conditions are col 17 = "P" and col 81 = "*", this will go to one output file; col 17 = "R" and col 81 = " ". Here is an example. ... (3 Replies)
Discussion started by: chanclitas
3 Replies

5. Shell Programming and Scripting

Divide data into separate files

frnds: i want to divide data on the behalf of dotted line and redirectd into new files ) ------------------------- M-GET CONFIRMATION ( ------------------------- M-GET CONFIRMATION ( INVOKE IDENTIFIER final data shuld be into 3 files ...... (6 Replies)
Discussion started by: dodasajan
6 Replies

6. UNIX for Dummies Questions & Answers

Help with varying data

I have flat file in which the length of the a record is 1000 characters. the last field of the file range is from 951 to 1000. So currently i am getting the last field data to be less than 1000 characters ( from 951 to 1000 i see that the data varies from 10 to 50). So is there a way we can pad up... (18 Replies)
Discussion started by: akshay01987
18 Replies

7. UNIX for Dummies Questions & Answers

How to Separate Odd and Even number from one data set?

Hi, If I want to separate data set to new file by odd and even number. If data set like this 1 ABC 235 hgf 2 DEF 326 kjk 3 XXX 133 kwd 4 YYY 188 fgh If I want separate by colum3 I want result like set 1 1 ABC 235 hgf 3 XXX 133 kwd put to new... (3 Replies)
Discussion started by: GeodusT
3 Replies

8. Shell Programming and Scripting

Using AWK to separate data from a large XML file into multiple files

I have a 500 MB XML file from a FileMaker database export, it's formatted horribly (no line breaks at all). The node structure is basically <FMPXMLRESULT> <METADATA> <FIELD att="............." id="..."/> </METADATA> <RESULTSET FOUND="1763457"> <ROW att="....." etc="...."> ... (16 Replies)
Discussion started by: JRy
16 Replies

9. Shell Programming and Scripting

Parse apart strings of comma separated data with varying number of fields

I have a situation where I am reading a text file line-by-line. Those lines of data contain comma separated fields of data. However, each line can vary in the number of fields it can contain. What I need to do is parse apart each line and write each field of data found (left to right) into a file.... (7 Replies)
Discussion started by: 2reperry
7 Replies

10. Shell Programming and Scripting

count data separate by comma

hi experts, i have some problem with count data which separate by comma, below sample data : 01,011222823b6d,011222823f29,0028a5,002993,6212345678, 659111111111,6598204507,6281105008,6596197849,_,525016160836958,_, ffffffff,000000000000000000000000,_,_,_,fd,fd,ff,00,1,0028a5-002993,_,... (10 Replies)
Discussion started by: bucci
10 Replies

Featured Tech Videos