Sponsored Content
Top Forums Shell Programming and Scripting editing line in text file adding number to value in file Post 302591530 by rmohanty on Thursday 19th of January 2012 08:02:36 PM
Old 01-19-2012
Hi,

You can use the below script.

Code:
y=1
while read line
do
fld1=`echo $line |cut -d' ' -f1|tr -d ' '`
if [ "$fld1" = 'Data' ]; then
   fld2=`echo $line|cut -d'"' -f2|cut -d'#' -f1|tr -d ' '`
   fld3=`expr $fld2 + 1000000`
   sed -i "$y s/$fld2/$fld3/" test1
elif [ "$fld1" = 'ID' ]; then
   fld2=`echo $line|cut -d' ' -f2|tr -d ' '`
   fld3=`expr $fld2 + 1000000`
   sed -i "$y s/$fld2/$fld3/" test1
fi
y=`expr $y + 1`
done < inputfile
exit 0

Code:
Input file :
 
Data "12345#22"
Fred
ID 12345
Age 45
Wilma
Dino
Data "123#22"
Tarzan
ID 123
Age 33
Jane

Code:
Output file : 
 
Data "1012345#22" 
Fred 
ID 1012345 
Age 45 
Wilma 
Dino 
Data "1000123#22" 
Tarzan 
ID 1000123 
Age 33 
Jane

If the final number has to be < 2000000 then you will need to put a check before using 'sed' so that the original value is not updated.

Regards,
RM

Last edited by rmohanty; 01-19-2012 at 10:53 PM..
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adding Text To each line of a file

How would I add text to the beginning of each line in a text file in a script right after the file is created from another text file. (4 Replies)
Discussion started by: cubs0729
4 Replies

2. Shell Programming and Scripting

adding text to end of each line in a file

I'm needing to add a "hour:min" to the end of each line in a document. The document in this case is only going to be one line. if this inserts it at the end, what needs to be changed to add something at the end... /bin/echo "%s/^/$filler/g\nwq!" | ex -s $oFile Thank you... (2 Replies)
Discussion started by: cubs0729
2 Replies

3. Shell Programming and Scripting

Adding specific text and spaces to each line in a text file

Hi, I wanted to add specific text to each row in a text file containing three rows. Example: 0 8 7 6 5 5 7 8 9 0 7 9 7 8 9 0 1 2 And I want to add a 21 at the beginning of the first row, and blank spaces at the beginning of the second two rows. To get this: 21 0 8 7 6 5 5 7 8... (4 Replies)
Discussion started by: hertingm
4 Replies

4. Linux

Get a specific line number from a text file

Hello! All, Could you please tell me how to get a specific line number from a text file? For example below, ABC DEF ---> Get this line number, return to an variable GHI My OS is Linux. Thank you so much for your help in advance! (3 Replies)
Discussion started by: barryxian
3 Replies

5. Shell Programming and Scripting

adding a line to a text file

I have a tab delimited text file, id name distance 1 3325167 0.334561754018 2 3290488 0.389444269458 3 3288794 0.392312701782 4 3347602 0.392532202097 5 3295355 0.394394169485 I need to add a line after the header line. The first and third field of... (3 Replies)
Discussion started by: LMHmedchem
3 Replies

6. Shell Programming and Scripting

adding line number to *end* of records in file

Given a file like this: abc def ghi I need to get to somestandardtext abc1 morestandardtext somestandardtext def2 morestandardtext somestandardtext ghi3 morestandardtext Notice that in addition to the standard text there is the line number added in as well. What I conceived is... (4 Replies)
Discussion started by: edstevens
4 Replies

7. Shell Programming and Scripting

Adding text to the end of the specific line in a file(only to the first occurrence of it)

Hi, I want to add a text to the end of the specific line in a file. Now my file looks like this: 999 111 222 333 111 444 I want to add the string " 555" to the end of the first line contaning 111. Moreover, I want to insert a newline after this line containg the "000" string. The... (8 Replies)
Discussion started by: wenclu
8 Replies

8. Shell Programming and Scripting

Adding filename and line number from multiple files to final file

Hi all, I have 20 files (file001.txt upto file020.txt) and I want to read them from 3rd line upto end of file (line 1002). But in the final file they should appear to start from line 1. I need following kind of output in a single file: Filename Line number 2ndcolumn 4thcolumn I... (14 Replies)
Discussion started by: bioinfo
14 Replies

9. UNIX for Dummies Questions & Answers

[Help] Adding text to a variable line in a file

Hey guys, I need to write a script that will add a specific text at the end of a specific line (of a text file). but the line is a variable this is my text file : device_2 ansible_ssh_host=127.0.0.1 ansible_ssh_port=30000 ansible_ssh_user='root' device_2 ansible_ssh_host=127.0.0.1... (1 Reply)
Discussion started by: OdedOvdat
1 Replies

10. Shell Programming and Scripting

Adding user name to file, and then displaying new line number

Hi all - I'm completely stumped by a script I'm working on... The short version is I have a file called 'lookup' and in it are hundreds of names (first and last). I have a script that basically allows the user to enter a name, and what I need to have happen is something like this: Record... (8 Replies)
Discussion started by: sabster
8 Replies
Boulder::Stream(3pm)					User Contributed Perl Documentation				      Boulder::Stream(3pm)

NAME
Boulder::Stream - Read and write tag/value data from an input stream SYNOPSIS
#!/bin/perl # Read a series of People records from STDIN. # Add an "Eligible" tag to all those whose # Age >= 35 and Friends list includes "Fred" use Boulder::Stream; # filestream way: my $stream = Boulder::Stream->newFh; while ( my $record = <$stream> ) { next unless $record->Age >= 35; my @friends = $record->Friends; next unless grep {$_ eq 'Fred'} @friends; $record->insert(Eligible => 'yes'); print $stream $record; } # object oriented way: my $stream = Boulder::Stream->new; while (my $record = $stream->get ) { next unless $record->Age >= 35; my @friends = $record->Friends; next unless grep {$_ eq 'Fred'} @friends; $record->insert(Eligible => 'yes'); print $stream $record; } DESCRIPTION
Boulder::Stream provides stream-oriented access to Boulder IO hierarchical tag/value data. It can be used in a magic tied filehandle mode, as shown in the synopsis, or in object-oriented mode. Using tied filehandles, Stone objects are read from input using the standard <> operator. Stone objects printed to the tied filehandle appear on the output stream in Boulder format. By default, data is read from the magic ARGV filehandle (STDIN or a list of files provided on the command line) and written to STDOUT. This can be changed to the filehandles of your choice. Pass through behavior When using the object-oriented form of Boulder::Stream, tags which aren't specifically requested by the get() method are passed through to output unchanged. This allows pipes of programs to be constructed easily. Most programs will want to put the tags back into the boulder stream once they're finished, potentially adding their own. Of course some programs will want to behave differently. For example, a database query program will generate but not read a boulderio stream, while a report generator will read but not write the stream. This convention allows the following type of pipe to be set up: query_database | find_vector | find_dups | | blast_sequence | pick_primer | mail_report If all the programs in the pipe follow the conventions, then it will be possible to interpose other programs, such as a repetitive element finder, in the middle of the pipe without disturbing other components. SKELETON BOULDER PROGRAM
Here is a skeleton example. #!/bin/perl use Boulder::Stream; my $stream = Boulder::Stream->newFh; while ( my $record = <$stream> ) { next unless $record->Age >= 35; my @friends = $record->Friends; next unless grep {$_ eq 'Fred'} @friends; $record->insert(Eligible => 'yes'); print $stream $record; } The code starts by creating a Boulder::Stream object to handle the I/O. It reads from the stream one record at a time, returning a Stone object. We recover the Age and Friends tags, and continue looping unless the Age is greater or equal to 35, and the list of Friends contains "Fred". If these criteria match, then we insert a new tag named Eligible and print the record to the stream. The output may look like this: Name=Janice Age=36 Eligible=yes Friends=Susan Friends=Fred Friends=Ralph = Name=Ralph Age=42 Eligible=yes Friends=Janice Friends=Fred = Name=Susan Age=35 Eligible=yes Friends=Susan Friends=Fred = Note that in this case only records that meet the criteria are echoed to standard output. The object-oriented version of the program looks like this: #!/bin/perl use Boulder::Stream; my $stream = Boulder::Stream->new; while ( my $record = $stream->get('Age','Friends') ) { next unless $record->Age >= 35; my @friends = $record->Friends; next unless grep {$_ eq 'Fred'} @friends; $record->insert(Eligible => 'yes'); $stream->put($record); } The get() method is used to fetch Stones containing one or more of the indicated tags. The put() method is used to send the result to standard output. The pass-through behavior might produce a set of records like this one: Name=Janice Age=36 Eligible=yes Friends=Susan Friends=Fred Friends=Ralph = Name=Phillip Age=30 = Name=Ralph Age=42 Eligible=yes Friends=Janice Friends=Fred = Name=Barbara Friends=Agatha Friends=Janice = Name=Susan Age=35 Eligible=yes Friends=Susan Friends=Fred = Notice that there are now two records ("Phillip" and "Barbara") that do not contain the Eligible tag. Boulder::Stream METHODS $stream = Boulder::Stream->new(*IN,*OUT) $stream = Boulder::Stream->new(-in=>*IN,-out=>*OUT) The new() method creates a new Boulder::Stream object. You can provide input and output filehandles. If you leave one or both undefined new() will default to standard input or standard output. You are free to use files, pipes, sockets, and other types of file handles. You may provide the filehandle arguments as bare words, globs, or glob refs. You are also free to use the named argument style shown in the second heading. $fh = Boulder::Stream->newFh(-in=>*IN, -out=>*OUT) Returns a filehandle object tied to a Boulder::Stream object. Reads on the filehandle perform a get(). Writes invoke a put(). To retrieve the underlying Boulder::Stream object, call Perl's built-in tied() function: $stream = tied $fh; $stone = $stream->get(@taglist) @stones = $stream->get(@taglist) Every time get() is called, it will return a new Stone object. The Stone will be created from the input stream, using just the tags provided in the argument list. Pass no tags to receive whatever tags are present in the input stream. If none of the tags that you specify are in the current boulder record, you will receive an empty Stone. At the end of the input stream, you will receive undef. If called in an array context, get() returns a list of all stones from the input stream that contain one or more of the specified tags. $stone = $stream->read_record(@taglist) Identical to get(>, but the name is longer. $stream->put($stone) Write a Stone to the output filehandle. $stream->write_record($stone) Identical to put(), but the name is longer. Useful State Variables in a Boulder::Stream Every Boulder::Stream has several state variables that you can adjust. Fix them in this fashion: $a = new Boulder::Stream; $a->{delim}=':'; $a->{record_start}='['; $a->{record_end}=']'; $a->{passthru}=undef; o delim This is the delimiter character between tags and values, "=" by default. o record_start This is the start of nested record character, "{" by default. o record_end This is the end of nested record character, "}" by default. o passthru This determines whether unrecognized tags should be passed through from the input stream to the output stream. This is 'true' by default. Set it to undef to override this behavior. BUGS
Because the delim, record_start and record_end characters in the Boulder::Stream object are used in optimized (once-compiled) pattern matching, you cannot change these values once get() has once been called. To change the defaults, you must create the Boulder::Stream, set the characters, and only then begin reading from the input stream. For the same reason, different Boulder::Stream objects cannot use different delimiters. AUTHOR
Lincoln D. Stein <lstein@cshl.org>, Cold Spring Harbor Laboratory, Cold Spring Harbor, NY. This module can be used and distributed on the same terms as Perl itself. SEE ALSO
Boulder, Boulder::Blast, Boulder::Genbank, Boulder::Medline, Boulder::Unigene, Boulder::Omim, Boulder::SwissProt perl v5.10.1 2001-06-11 Boulder::Stream(3pm)
All times are GMT -4. The time now is 10:50 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy