- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- file manipulation scripting
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
04-24-2003 04:08 AM
04-24-2003 04:08 AM
file manipulation scripting
I am new to unix so be kind. I have a text file that I need to modify. Each line in the file consists of groups of data to be read by another program. What I need to do is to add a new group at the beginning of each line in the form:
IIP## , where ## is a sequence number that is incremented for each line. I also need to append an "=" character to the last group of each line. The number of lines in each file is variable.
Can this be easily done with scripting or do I need to write a program to do this?
Greg Wright
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-24-2003 04:12 AM
04-24-2003 04:12 AM
Re: file manipulation scripting
This can be shell scripted, perl'd, C'd, etc'd.
Use what you're comfortable with; use what you know. If you don't know any of the aforementioned, use shell or perl.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-24-2003 04:15 AM
04-24-2003 04:15 AM
Re: file manipulation scripting
As mentioned, loads of ways, but here's an awk method:
awk '{printf("IIP%02d %s=\n",NR,$0)}' filename
rgds, Robin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-24-2003 04:40 AM
04-24-2003 04:40 AM
Re: file manipulation scripting
while (<>) {
chomp; # strip record separator
printf "IIP%02d %s=\n", $., $_;
print $_ if $filename;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-24-2003 04:43 AM
04-24-2003 04:43 AM
Re: file manipulation scripting
a simple standard script can probably also do it, e.g.:
#!/usr/bin/sh
typeset -i NUMBER=0
while read line
do
let NUMBER="$NUMBER + 1"
echo "IIP${NUMBER} ${line}="
done <$1
Run it like this:
# simple_script.sh inputfile >new_out
to redirect the result to new_out
regards,
John K.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-24-2003 05:05 AM
04-24-2003 05:05 AM
Re: file manipulation scripting
He asked us to be nice !
Looking at just one file you want to look at :
#!/opt/perl/bin/perl
$file="/etc/hosts";
$file2="/tmp/file2";
# Open source file for read only mode
open(FH, "< $file");
# Open destination file in write mode
open(FD, "> $file2");
$num = 0;
while(
# Set $num to value + 1
$num++;
# Add the number to the beginning of the line
$_ = "IIP $num" . $_ ;
# Add a = to the end of the line
s/\n//;
$_ = $_ . "\= \n";
# Now store the current line into file2
print FH "$_" ;
# An print it on screen
print $_;
} # end of the while loop (so end of file)
# Dont forget to close both Files
close(FH);
close(FD);
print "Alright, done checking $file. \n";
print "File had $num lines \n"
----------------------
If you need any more info, please let us know.
Best Regs David
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-24-2003 05:28 AM
04-24-2003 05:28 AM
Re: file manipulation scripting
Greg
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-24-2003 06:37 AM
04-24-2003 06:37 AM
Re: file manipulation scripting
Since I do not have PERL on my system I tried the AWK and shell script methods. Both worked, or would have if I had included the following information. Each line ends with an CR character. This causes the AWK method to place the '=' character at the beginning of each line (overwriting the first I in IIP) and the shell scripting method to place the '=' after the CR character.
In the AWK method I tried \b between the %s and = characters but it didn't work.
Any further solutions? I need method of stripping out the CR at the end of each line.
Greg
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-24-2003 06:46 AM
04-24-2003 06:46 AM
Re: file manipulation scripting
Perl is free, and IMHO indispensable for an SA.
Get it (perl 5.8.0) from your latest Application CD's, from http://hpux.connect.org.uk/hppd/hpux/Languages/perl-5.8.0/ or from https://www.beepz.com/personal/merijn/ or http://www.cmve.net/~merijn/
and enjoy it's rich set of features.
Enjoy, have FUN! H.Merijn
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-24-2003 06:48 AM
04-24-2003 06:48 AM
Re: file manipulation scripting
If you have carriage return characters in your file then you undoubtedly transferred it from Windows using FTP but in binary mode. Text file transfers from Unix to/from Windows should be done in ASCII mode. This converts the Windows linefeed/carriage-return to a Unix newline character and vice versa.
You can also correct your file on the Unix server with:
# dos2ux < filename > filename.new
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-24-2003 06:50 AM
04-24-2003 06:50 AM
Re: file manipulation scripting
A stupid one
mv file /tmp/file
nl -ba -nln /tmp/file|while read line
do
echo "IIP"$line"="
done > file
/bin/rm /tmp/file
Steve Steel
Do not make our input pointless. Give points to the ones you like.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-24-2003 07:29 AM
04-24-2003 07:29 AM
Re: file manipulation scripting
You could also remove the CRs within awk:
awk '{sub("\r","");printf("IIP%02d %s=\n",NR,$0)}' filename
rgds, Robin