- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- how to reformat flat file using cut command
Operating System - HP-UX
1752407
Members
5784
Online
108788
Solutions
Forums
Categories
Company
Local Language
юдл
back
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
юдл
back
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
Discussion Boards
Discussion Boards
Blogs
Information
Community
Resources
Community Language
Language
Forums
Blogs
Go to solution
Topic Options
- 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
тАО10-08-2010 08:04 AM
тАО10-08-2010 08:04 AM
Hi
today I got some new requirement ,I am not able to get the solution for this using
awk or cut command please help me.
Problem: I have given sample input file below,
I/p file
-------------------------------------------
9029774138: "B2", 1;20101009;50791.00|2;20101009;698.77|3;20290309;862655.00|4;20290309;454646.00|
5;20290309;94.00|10;20101005;0.00|11;20101103;0.00|15;20291112;47.00|16;20291203;55.00|
17;20101017;0.00
Required output file
---------------------------
9029774138:"B2":10:1,2,3,4,5,10,11,15,16,17
--------------------------------------------
out put is a colon seperated file first two fields are same as first two fields in input file, third record is the count (number of pipe symbols + 1) fourth record is string seperated by comma which contains each field after the pipe symbol .
Thanks in Anticipation
Gopi
today I got some new requirement ,I am not able to get the solution for this using
awk or cut command please help me.
Problem: I have given sample input file below,
I/p file
-------------------------------------------
9029774138: "B2", 1;20101009;50791.00|2;20101009;698.77|3;20290309;862655.00|4;20290309;454646.00|
5;20290309;94.00|10;20101005;0.00|11;20101103;0.00|15;20291112;47.00|16;20291203;55.00|
17;20101017;0.00
Required output file
---------------------------
9029774138:"B2":10:1,2,3,4,5,10,11,15,16,17
--------------------------------------------
out put is a colon seperated file first two fields are same as first two fields in input file, third record is the count (number of pipe symbols + 1) fourth record is string seperated by comma which contains each field after the pipe symbol .
Thanks in Anticipation
Gopi
Solved! Go to Solution.
3 REPLIES 3
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-08-2010 08:38 AM
тАО10-08-2010 08:38 AM
Re: how to reformat flat file using cut command
The pipe character is a rather evil separator in shell. Lots of ways to do anything on unix. Call this one the shell IFS example:
# convertStuff() {
-> IFS="#"
->
-> for X in "${@}"
-> do
-> echo $X
-> done
-> }
# convertStuff $( cat file | sed -e "s/\|/#/g" )
1;20101009;50791.00
2;20101009;698.77
3;20290309;862655.00
4;20290309;454646.00
5;20290309;94.00
10;20101005;0.00
11;20101103;0.00
15;20291112;47.00
16;20291203;55.00
17;20101017;0.00
Using that trick a pure sh solution is an easy exercise for the reader...
Bets on time until a Perl solution is posted and who does it? :-)
# convertStuff() {
-> IFS="#"
->
-> for X in "${@}"
-> do
-> echo $X
-> done
-> }
# convertStuff $( cat file | sed -e "s/\|/#/g" )
1;20101009;50791.00
2;20101009;698.77
3;20290309;862655.00
4;20290309;454646.00
5;20290309;94.00
10;20101005;0.00
11;20101103;0.00
15;20291112;47.00
16;20291203;55.00
17;20101017;0.00
Using that trick a pure sh solution is an easy exercise for the reader...
Bets on time until a Perl solution is posted and who does it? :-)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-08-2010 08:44 AM
тАО10-08-2010 08:44 AM
Solution
to 'walk' the list you'll have to write a little program in the language of your choice.
C, shell, perl, awk.
Using AWK, the following seems to work:
---------- test.awk --------
{
values = split ($3, a, "|");
line = $1 $2 values ":";
for (i = 1; i < values; i++) {
split (a[i], b, ";");
line = line b[1] ",";
}
split (a[values], last_one, ";");
print line last_one[1];
}
----------------
use as : # awk -f test.awk test.txt
The program splits $3 into an array 'a'
It starts by building the first part of the line using $1, $2 and the returned count for the split.
Then it walks all but the last element from 'a' to split again on ";" into array 'b'.
Element 1 in 'b' has the value.
Glue that to the line, and add a comma.
Add the last one separately to avoid the extra comma.
Enjoy,
Hein
C, shell, perl, awk.
Using AWK, the following seems to work:
---------- test.awk --------
{
values = split ($3, a, "|");
line = $1 $2 values ":";
for (i = 1; i < values; i++) {
split (a[i], b, ";");
line = line b[1] ",";
}
split (a[values], last_one, ";");
print line last_one[1];
}
----------------
use as : # awk -f test.awk test.txt
The program splits $3 into an array 'a'
It starts by building the first part of the line using $1, $2 and the returned count for the split.
Then it walks all but the last element from 'a' to split again on ";" into array 'b'.
Element 1 in 'b' has the value.
Glue that to the line, and add a comma.
Add the last one separately to avoid the extra comma.
Enjoy,
Hein
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-11-2010 04:06 AM
тАО10-11-2010 04:06 AM
Re: how to reformat flat file using cut command
ThanQ Hein,
It is very useful. Once again Thanks for your solution.
Cheers,
Gopi
It is very useful. Once again Thanks for your solution.
Cheers,
Gopi
The opinions expressed above are the personal opinions of the authors, not of Hewlett Packard Enterprise. By using this site, you accept the Terms of Use and Rules of Participation.
News and Events
Support
© Copyright 2024 Hewlett Packard Enterprise Development LP