Operating System - HP-UX
1829159 Members
2049 Online
109986 Solutions
New Discussion

file manipulation scripting

 
Gregory F. Wright
Occasional Advisor

file manipulation scripting

Hi,
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
11 REPLIES 11
Christopher Caldwell
Honored Contributor

Re: file manipulation scripting

Let me count the ways ...

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.

Robin Wakefield
Honored Contributor

Re: file manipulation scripting

Hi Greg,

As mentioned, loads of ways, but here's an awk method:

awk '{printf("IIP%02d %s=\n",NR,$0)}' filename

rgds, Robin
Paddy_1
Valued Contributor

Re: file manipulation scripting

in perl you can do something like

while (<>) {
chomp; # strip record separator
printf "IIP%02d %s=\n", $., $_;
print $_ if $filename;
}
The sufficiency of my merit is to know that my merit is NOT sufficient
john korterman
Honored Contributor

Re: file manipulation scripting

Hi Greg,
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.
it would be nice if you always got a second chance
David_246
Trusted Contributor

Re: file manipulation scripting

He guys,

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
@yourservice
Gregory F. Wright
Occasional Advisor

Re: file manipulation scripting

I want to thank everyone who replied. All of your suggestions were helpful. I see that I need to get some good learning sources on PERL and shell scripting if I am going to be working in UNIX.

Greg
Gregory F. Wright
Occasional Advisor

Re: file manipulation scripting

Hi again,

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
H.Merijn Brand (procura
Honored Contributor

Re: file manipulation scripting

You don't have perl? Uhh, look again, you *do* have perl (/usr/contrib/bin/perl), but that is an old version (version 4).

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
Enjoy, Have FUN! H.Merijn
James R. Ferguson
Acclaimed Contributor

Re: file manipulation scripting

Hi Greg:

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...
Steve Steel
Honored Contributor

Re: file manipulation scripting

Hi

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.

If you want truly to understand something, try to change it. (Kurt Lewin)
Robin Wakefield
Honored Contributor

Re: file manipulation scripting

Hi Greg,

You could also remove the CRs within awk:

awk '{sub("\r","");printf("IIP%02d %s=\n",NR,$0)}' filename

rgds, Robin