- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: field format
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
Discussions
Discussions
Forums
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
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
тАО06-11-2009 06:17 AM
тАО06-11-2009 06:17 AM
I have a file in the format
1,type1,20090510,20090511,1000,1100
2,type2,20090611,20090714,1200,1300
3,type3,20090414,20090515,1600,1700
I want the file to be present in this format:
type1,2009-05-10 10:00,2009-05-11 11:00
type2,2009-06-11 12:00,2009-07-14 13:00
type3,2009-04-14 16:00,2009-05-15 17:00
i.e the first field to be removed and the date and time stamps to be merged.Please help
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-11-2009 06:32 AM
тАО06-11-2009 06:32 AM
Re: field format
awk -F, '{print $2","$3,$5","$4,$6}' /file
should do the trick.
Richard
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-11-2009 06:33 AM
тАО06-11-2009 06:33 AM
Re: field format
typ=`echo $line | cut -d"," -f2`
date1=`echo $line | cut -d"," -f3`
date2=`echo $line | cut -d"," -f4`
time1=`echo $line | cut -d"," -f5`
time2=`echo $line | cut -d"," -f6`
y1=`echo $date1 | cut -c 1-4`
m1=`echo $date1 | cut -c 5-6`
d1=`echo $date1 | cut -c 7-8`
h1=`echo $time1 | cut -c 1-2`
mm1=`echo $time1 | cut -c 3-4`
y2=`echo $date2 | cut -c 1-4`
m2=`echo $date2 | cut -c 5-6`
d2=`echo $date2 | cut -c 7-8`
h2=`echo $time2 | cut -c 1-2`
mm2=`echo $time2 | cut -c 3-4`
echo "${typ},${y1}-${m1}-${d1} ${h1}:${mm1},${y2}-${m2}-${d2} ${h2}:${mm2}"
done
Hope this helps.. it is not most elegant but easy to follow...
UNIX because I majored in cryptology...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-11-2009 06:35 AM
тАО06-11-2009 06:35 AM
Re: field format
Thanks for the hint.
Hi Mel,
I have around 1000 records in the file. So opening up a new variable for each record is impossible.
Please suggest something quick as i have 1000 records in the file.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-11-2009 06:38 AM
тАО06-11-2009 06:38 AM
Re: field format
UNIX because I majored in cryptology...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-11-2009 06:38 AM
тАО06-11-2009 06:38 AM
Re: field format
# awk -F"," 'BEGIN{OFS=","};{print $2,substr($3,1,4)"-"substr($3,5,2)"-"substr($3,7,2),substr($5,1,2)":"substr($5,3,2),substr($4,1,4)"-"substr($4,5,2)"-"substr($3,7,2),substr($6,1,2)":"substr($6,3,2)}' file
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-11-2009 06:42 AM
тАО06-11-2009 06:42 AM
SolutionOops, you want commas only at selected points, so:
# awk -F, {print $2","substr($3,1,4)"-"substr($3,5,2)"-"substr($3,7,2),substr($5,1,2)":"substr($5,3,2)","substr($4,1,4)"-"substr($4,5,2)"-"substr($3,7,2),substr($6,1,2)":"substr($6,3,2)} file
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-11-2009 06:43 AM
тАО06-11-2009 06:43 AM
Re: field format
that was splendid!
i have few more fields after the time stamp like,
1,type1,20090510,20090511,1000,1100,D,34.0,,,MI
2,type2,20090611,20090714,1200,1300,,D,78.0,,,KI
3,type3,20090414,20090515,1600,1700,,K,90.8,,LI
How do i accomodate if the fields are more.
Thanks in advance.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-11-2009 06:53 AM
тАО06-11-2009 06:53 AM
Re: field format
> I have few more fields after the time stamp ...How do i accomodate if the fields are more.
You simply reference fields in'awk' as $0 (for the whole line) and $1..$199 for the first through the 199th field. Awk automatically splits a line into fields based on the record seperator. For commandline scripts you can use the '-F' switch like I did to designate it. The manpages offer an overview of functions like 'substr()' which we used to dissect fixed spans of various fields.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-11-2009 11:31 PM
тАО06-11-2009 11:31 PM
Re: field format
to get the rest of the fields unchanged in your output, use
# awk -F, '{printf("%s,%s-%s-%s %s:%s, %s-%s-%s %s:%s",
$2,substr($3,1,4),substr($3,5,2),substr($3,7,2),
substr($5,1,2),substr($5,3,2),
substr($4,1,4),substr($4,5,2),substr($3,7,2),
substr($6,1,2),substr($6,3,2));
for(i=7;i<=NF;i++) printf(",%s",$i); printf("\n")}' file
mfG Peter