loop through file to change some data


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting loop through file to change some data
# 1  
Old 05-01-2009
loop through file to change some data

Hi, I have a file with the following data
-0.00000 0.00000 0.00000 F F F
0.00000 0.00000 0.80000 F F F
0.50000 0.50000 0.60000 F F F
0.50000 0.50000 0.20000 F F F
-0.00000 0.00000 0.40000 F F F

I would like to change the last 3 lines from F F F to T T T. I tried looping each line but don't know how to tell it to change the last 3 lines. I wrote this (below) and it changed all the F F F to T T T

for i in tet1
do sed 's/F/T/g' tet1 > tet2
done


How do I change the last 3 lines pls. Thanks in advance
# 2  
Old 05-01-2009
You need to know the last three lines ahead of time
Code:
wc -l tet1 | read lines fname
lines=$(( $lines -2 ))
awk -v lines=$lines ' {  if (NR >= lines) {print $1, $2, $3, "T T T"}
                               else {print $0} 
                            }' tet1 > tet2

# 3  
Old 05-01-2009
Hammer & Screwdriver in my bash shell, the following works

Code:
> cat file14
-0.00000 0.00000 0.00000 F F F
0.00000 0.00000 0.80000 F F F
0.50000 0.50000 0.60000 F F F
0.50000 0.50000 0.20000 F F F
-0.00000 0.00000 0.40000 F F F

> cat -n file14 | tail -3 | awk '{print $1,$2,$3,$4,"T T T"}' >file14.t ;cat -n file14 | head -2 >>file14.t ; sort file14.t | awk '{print $2,$3,$4,$5,$6,$7}'
-0.00000 0.00000 0.00000 F F F
0.00000 0.00000 0.80000 F F F
0.50000 0.50000 0.60000 T T T
0.50000 0.50000 0.20000 T T T
-0.00000 0.00000 0.40000 T T T

# 4  
Old 05-01-2009
Code:
wc -l filename| while read line name
do 
line=$(( $line - 2 ))
awk -v line=$line '{if(NR >= line){gsub(/F/,"T",$0);print
}
else{print}}' filename
done

cheers,
Devaraj Takhellambam
# 5  
Old 05-01-2009
Code:
$
$ cat test1.txt
-0.00000 0.00000 0.00000 F F F
0.00000 0.00000 0.80000 F F F
0.50000 0.50000 0.60000 F F F
0.50000 0.50000 0.20000 F F F
-0.00000 0.00000 0.40000 F F F
$
$ perl -e '{
>   open (F,"test1.txt"); @file = <F>; close (F);
>   for ($i=0; $i<$#file+1; $i++) {
>     if ($i>$#file-3) { $file[$i] =~ s/F/T/g; }
>     print $file[$i]
>   }
> }'
-0.00000 0.00000 0.00000 F F F
0.00000 0.00000 0.80000 F F F
0.50000 0.50000 0.60000 T T T
0.50000 0.50000 0.20000 T T T
-0.00000 0.00000 0.40000 T T T
$
$

HTH,
tyler_durden

______________________________________________
"Only after disaster can we be resurrected."
# 6  
Old 05-02-2009
MySQL It worked for me

Try this one
Code:
cat input | awk 'BEGIN{FS="\n";lines=3}{for (i=1;i<=NF;i++) if (NR >= lines) {gsub(/F/,"T",$0); print} else {print $i}}'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need to loop file data based on delimiter

My file has data that looks like below: more data.txt I wish to display each string seperated by a delimiter : Expected output: I tried the below but I m not getting every split string on a new line. #!/bin/bash for i in `sed 's/:/\\n/g' data.txt`; do echo -n... (2 Replies)
Discussion started by: mohtashims
2 Replies

2. Shell Programming and Scripting

Doing a loop to change a value in a file

Hi, I have an N number of files in a directory. I like to write a shell script that would make identical plots for each one of these files. The files have names such as: t00001.dat t00002.dat t00003.dat t00004.dat t00005.dat . . . t00040.dat i.e. the... (4 Replies)
Discussion started by: lost.identity
4 Replies

3. Shell Programming and Scripting

Change data in file

Change File data Input A.txt LL725 LL725_A LL725 SIB.ra=6 LL725 LL725_B LL725 SIB.ra=11 LL726 LL726_A LL726 SIB.ra=20 LL726 LL726_B LL726 SIB.ra=6 LL726 LL726_C LL726 SIB.ra=10 Output :- LL725 LL725_A SIB.ra=6 LL725 LL725_B SIB.ra=11 LL726 LL726_A SIB.ra=20 (4 Replies)
Discussion started by: pareshkp
4 Replies

4. Shell Programming and Scripting

Running ffmpeg in loop seems to change reading from file

Hi everyone, I am fairly new to shell scripting. I want to read in numbers from a file (one number per line). This works perfectly fine while read CurrentLine do echo $CurrentLine done < myfile and yields the correct output: 272 745 123 If I however run a ffmpeg... (2 Replies)
Discussion started by: Thriceguy
2 Replies

5. Shell Programming and Scripting

Help i want to change the data of one file and apend it into 2nd file.

Hi All, Please help i have written an ksh script, where i am actually take count of lines in one file and want to update this count to 2nd field of a new file and apend the this into an existing file. Note the below script is in for loop -------- I am apending few records in a file... (7 Replies)
Discussion started by: iamnoone
7 Replies

6. Shell Programming and Scripting

Execution Problems with scan and change file data content

Input file >Read_1 XXXXXXXXXXSDFXXXXXDS ASDRXXXXXTGAGTXXXXXT TGTGATXXXXXAXXXXGXXA . . Desired output file >Read_1 XXXXXXXXXXXXXXXXXXDS ASDRXXXXXTGAGTXXXXXT TGTGATXXXXXXXXXXXXXA . . (5 Replies)
Discussion started by: patrick87
5 Replies

7. Shell Programming and Scripting

Scan and change file data content problem

Input file >Read_1 XXXXXXXXXXSDFXXXXXDS (condition 1: After the last "X" per line, if the distance is less than or equal to 3 letter, replace those not "X" letter with "X") TREXXXXXXXSDFXXXXXDS (condition 2: Before the first "X" per line, if the distance is less than or equal to 3 letter,... (12 Replies)
Discussion started by: patrick87
12 Replies

8. Shell Programming and Scripting

Change file content based on data

I have a Transaction File coming into the system. In this file, in all records the relevant data is as follows- Position 1:10 -> Transaction Code Position 252:255 -> 4 digit business code Now based on these 2 fields I have to alter value in Transaction code (Position 1:10)... (6 Replies)
Discussion started by: varunrbs
6 Replies

9. Shell Programming and Scripting

Get data from two text file in one loop

Hi all I have two file that contain different data.I want to get each file data line by line in one loop.i try with cat but cat works ok against one file File a 23 34 45 File b abc xyz cgh now i want like this in a loop.Below is just example.i am doing some thing smiler.can any... (3 Replies)
Discussion started by: aliahsan81
3 Replies

10. Shell Programming and Scripting

Using loop reading a file,retrieving data from data base.

Hi All, I am having trouble through, I am reading the input from tab delimited file containing several records, e.g. line1 field1 field2 field3 so on.. line2 field1 field2 field3 so on.. .. .. on the basis of certain fields for each record in input file, I have to retrieve... (1 Reply)
Discussion started by: Sonu4lov
1 Replies
Login or Register to Ask a Question