- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: reformat the flatfile in unix using cut comman...
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
тАО01-27-2008 06:46 AM
тАО01-27-2008 06:46 AM
I have a flatfile called a_M having 2 records in it the below content wherein the single record is scattered into 2 lines.
a_M
====
0001 13 3 HP Product and Pricing System ^M
0001 16 5 HP Product ^M
I need to reformat it and create a new file called b_M.$timestamp.
For this I have created a unix shell script with the below commands. But inthe output file i am getting some junk values. It looks if there is a space in the line the cut command does not take the proper column value.
Could anyone please help me out on this as it is very urgent.
#!/bin/ksh
for i in `cat a_M`
do
b=`echo $i | cut -c1-4`
c=`echo $i | cut -c17-50`
echo "FAM;A;"$b";";"$c >> b_M
done
timestamp=$( date +%d%m%y%H%M%S )
mv b_M b_M."$timestamp"
#END OF PROG
The ideal output should be
=============================
FAM;A;0001;HP Product and Pricing System
FAM;A;0001;HP Product
Thanks
Sasi
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-27-2008 07:24 AM
тАО01-27-2008 07:24 AM
Re: reformat the flatfile in unix using cut command
You have several problems. Your 'for' loop will execute for every "word" in the file --- not what you want. Instead you want to read a line at a time. You could do this:
#!/usr/bin/sh
while read i
do
b=`echo $i | cut -c1-4`
c=`echo $i | cut -c11-50`
echo "FAM;A;"$b";"$c
done < a_M > b_M
Notice that the input is read from the file named "a_M" into the variable "i" one line at a time. Any output of the 'while' loop is redirected to the file named "b_M".
You show a "^M" in your input file. This is a carriage-return character and means that your file came from a Windows platform. UNIX doesn't end lines with the carriage-return plus newline characters; only with a newline. You can strip these by doing:
# dos2ux a_M > a_M.fixed
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-27-2008 08:31 PM
тАО01-27-2008 08:31 PM
Re: reformat the flatfile in unix using cut command
Taking the comments made by JRF about dos2ux(1):
timestamp=$(date +%d%m%y%H%M%S)
dos2ux a_M | awk -v OFS=";" '
{
num = substr($0, 1, 4)
name = substr($0, 11, 40)
print "FAM;A", num, name
} ' > b_M.$timestamp
>echo "FAM;A;"$b";";"$c >> b_M
You shouldn't "stutter" your quoting, do it in one go:
echo "FAM;A;$b;$c"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-27-2008 09:16 PM
тАО01-27-2008 09:16 PM
Re: reformat the flatfile in unix using cut command
Could you please tell me if i need to code this awk command in unix shell script?
And moreover could you please tell me what does OFS mean?
does substr($0,11,40) give 11th character to 40th character of each line in the input file?
Could you please clarify these.
Thanks
Sasi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-28-2008 01:51 AM
тАО01-28-2008 01:51 AM
SolutionYou might as well, that was my intention.
>And moreover could you please tell me what does OFS mean?
Output Field Separator, that supplies the ";".
>does substr($0,11,40) give 11th character to 40th character of each line in the input file?
No, it starts at 11 for 40 chars. Indexing starts at 1. (Your original question said 17 but JRF had 11.)
http://docs.hp.com/en/B2355-60130/awk.1.html
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-28-2008 01:58 AM
тАО01-28-2008 01:58 AM
Re: reformat the flatfile in unix using cut command
In unix itself I could achieve this as below. The space was considered when I use "$i" in echo command. Previously I didnt use "" for $i. Hence the problem. Thank you for your help.
#!/usr/bin/ksh
while read i
do
b=`echo "$i" | cut -c1-4`
c=`echo "$i" | cut -c11-50`
echo "FAM;A;"$b";"$c
done < a_M > b_M
Thanks
Sasi