- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Shell program
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
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
06-22-2002 07:41 AM
06-22-2002 07:41 AM
what I need to do is this.
1. Read a file called Mar28.csv, the file contains only two filed. For example,
Citi Bank Payment,-30
Deposit,1349.2
2. If the second field contains a negative amount then I need to change it to
Citi Bank Payment,,-30
else if the second field is a positive number then I need to change it to
Deposit,1349.2,
I am trying to do this using shell programming.
I have been going round and round in circle for a while now without much success and if someone is kind enough to show it to me by giving an example I would really appreciate it.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-22-2002 07:59 AM
06-22-2002 07:59 AM
SolutionTo pick up the negative amount and add an extra , then:-
cat Mar28.csv | sed 's/,-/,,-/' >Mar28.csv-new
This will change
Citi Bank Payment,-30
to:-
Citi Bank Payment,,-30
Can you attach and example of your cvs file?
Paula
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-22-2002 08:03 AM
06-22-2002 08:03 AM
Re: Shell program
Off the top of my head, following is one most inefficent way which I typed in a rush (untested):
#!/sbin/sh
lineno=1
lastline=`wc -l $1|awk '{print $1}'`
while [ "$lineno" -le "$lastline" ]
do
line=`head -$lineno $1|tail -1`
if echo $line|awk -F\, '{print $2}'|grep '\-' >/dev/null
then
echo $line|awk -F\, '{print $1,",,",$2}'
else
echo $line
fi
lineno=`expr $lineno + `
done
Hope this helps. Regards.
Steven Sim Kok Leong
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-22-2002 08:06 AM
06-22-2002 08:06 AM
Re: Shell program
Typo in my quick draft of a script:
lineno=`expr $lineno + `
should be:
lineno=`expr $lineno + 1`
Hope this helps. Regards.
Steven Sim Kok Leong
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-22-2002 08:10 AM
06-22-2002 08:10 AM
Re: Shell program
bank.sh < Mar28.csv > Newfile
STAT=$?
if ${STAT} is non-zero, bad input data was detected.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-22-2002 01:11 PM
06-22-2002 01:11 PM
Re: Shell program
if ($2<0) printf ",%s\n",$2
else printf "%s,\n",$2
}' INPUTFILE
Regards,
Ceesjan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-22-2002 03:52 PM
06-22-2002 03:52 PM
Re: Shell program
perl -pie 's/[^,],-/,,-/' thefile.csv
This will make the change and put the results back into the same file. The additional feature this has over Paula's is that if you forget and run the process twice on the same file, it will leave the file in the proper format. Paula's would just keep adding commas everytime is was run.
perl... Try it.. You'll like it...
-- Rod Hills
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-23-2002 05:36 PM
06-23-2002 05:36 PM
Re: Shell program
I appreciate your feedback very much, and so everyone gets 10 points. However, to be frank with you lot of the stuff you said, went over my head because I am just starting out learning UNIX. :-) However, I ended up using a much simpler approach using awk utility. It is so simple it is amazing :-) and makes me proud of myself!! You won't believe it but this one line did all the work for me!
awk -F',' '{if ($2 > 0) {print $2","} else {print ","$2}}' Mar28.csv
Are you surprised? Thanks guys...you all are awesome!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-23-2002 06:48 PM
06-23-2002 06:48 PM
Re: Shell program
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-23-2002 11:04 PM
06-23-2002 11:04 PM
Re: Shell program
Do you see any differences between your solution and mine? It's a good thing not to assign points to yourself.
Keep dreaming...