- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- file sorting question
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2004 03:09 PM
02-25-2004 03:09 PM
easy one here, its 4:00am and I can't seem to think!!
I have a file that contains
123
456
789
etc etc
I need to read the file and output
c1t2d3
etc etc
My problem is I have no delimeter and am struggling
cheers
Steve
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2004 03:20 PM
02-25-2004 03:20 PM
Solutionfor i in $(cat file)
do
C=$(echo ${i} | cut -c 1)
D=$(echo ${i} | cut -c 2)
T=$(echo ${i} | cut -c 3)
DEVICE=c${C}d${D}t${T}
done
I think that should work.
Good luck
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2004 03:25 PM
02-25-2004 03:25 PM
Re: file sorting question
echo ${DEVICE}
I just did a quick test and that does work.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2004 03:28 PM
02-25-2004 03:28 PM
Re: file sorting question
should have thought of that really as I am using a cut in the preceding pipe to get the above info...doh!
Steve
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2004 04:28 PM
02-25-2004 04:28 PM
Re: file sorting question
My 2 cents.
If you are the one who is generating the input file, then I suggest you add a delimiter for c,t and d values. For ex., if you are making a file using the device files c1t2d3, c2t10d4 and c12t0d1, then your input file will become
123
2104
1201
It's just difficult to get back the original device files from the above. Where in, if you put a delimiter like :
1:2:3
2:10:4
12:0:1
It's really easy to get back the original device files.
I don't know your requirements but I am just cautioning for such cases in future.
-Sri
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2004 05:08 PM
02-25-2004 05:08 PM
Re: file sorting question
for i in $(cat file)
do
device=$(echo ${i} | sed -e 's/\(.\)\(.\)\(.\)/c\1t\2d\3/')
print $device
done
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2004 05:36 PM
02-25-2004 05:36 PM
Re: file sorting question
I had to offer this perl version if only so that I can learn something when Merijn pops in and does it using some perl construct I've never heard of.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2004 05:42 PM
02-25-2004 05:42 PM
Re: file sorting question
sed 's/\(.\)\(.\)\(.\)/c\1t\2d\3/g'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2004 06:53 PM
02-25-2004 06:53 PM
Re: file sorting question
But mind: you don't have to do the for loop. Just do: cat $file | sed ...
Or even:
sed ... < $file
One thing to bear in mind: it only will work with target ID's below 10.
If you have large target id's, the length of the string can be 3 or 4 characters, meaning the sed statement has to be changed:
cat $file | sed 's/^\(.\)\(.*\)\(.\)$/c\1t\2d\3/'
Which will put the first and last character in the buffers 1 and 3 and all characters in between in buffer 2. That is also why you will need the carrot and dollar sign now.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2004 07:21 PM
02-25-2004 07:21 PM
Re: file sorting question
playing golf, one can loose the anchors
# sed 's/\(.\)\(.*\)\(.\)/c\1t\2d\3/'
in perl one can loose more characters, but in basics it is the same
# perl -pe's/(.)(.)(.)/c$1t$2d$3/' file
obfuscation only makes it longer. But if you *know* that each line always is ^\d\d\d$, you can play a wicked game by dropping the last capture:
# perl -pe's/(.)(.)/c$1t$2d/' file
Enjoy, Have FUN! H.Merijn