Rows manupulation using AWK or sed


 
Thread Tools Search this Thread
Operating Systems AIX Rows manupulation using AWK or sed
# 1  
Old 12-09-2010
Rows manupulation using AWK or sed

Hi Everyon,

I am stuck in a script.I have a file named file1.txt as given below:
It contains 2 columns-count and filename.

cat file1.txt
count filename
100 A_new.txt
1000 A_full.txt
1100 B_new.txt
2000 B_full.txt
1100 C_new.txt
2000 C_full.txt
...................
..................

I want to subtract first field of row1 and row2 and store a result in a
variable say "diff". Then
I want to subtract first field of row3 and row4 and store a result in the
variable say "diff". Similary...for rows 5 and 6 and soon......


I am using kshell. I know I have to use AWK or sed but dont know how to.
Any help is apreciated.

Regards,
raj
# 2  
Old 12-09-2010
what exactly u want to do? do u want to overwrite the diff variable or want to store it in a file with specific format?
R0H0N
# 3  
Old 12-09-2010
Hi ROHON,

Thanks for quick reply.

I want to see the difference.After echoing diff, we can overwrtie it.


Regards,
raj.
# 4  
Old 12-09-2010
Code:
#!/bin/ksh

xargs -n2 < file1.txt | while read line
do
    set `echo $line | awk -F" " '{print $1 $3}'`
    new=$1 ; full=$2
    diff=$((full - new))
    echo "Diff=$diff"
done

Explanation:
Code:
xargs -n2 < file1.txt

this will generate following output

Code:
100 A_new.txt 1000 A_full.txt
1100 B_new.txt 2000 B_full.txt
1100 C_new.txt 2000 C_full.txt

so I m reading all lines one by one and fetching 1st and 3rd value of a line separated by space. after than just echoing the diff as per your requirement.

Last edited by R0H0N; 12-09-2010 at 02:32 AM..
This User Gave Thanks to For This Post:
R0H0N
# 5  
Old 12-09-2010
Try...
Code:
 
awk 'NR>1{s=$1;getline;print "Diff =" s-$1}' infile

# 6  
Old 12-09-2010
Hi ROHON,

Thanks for the reply.
I was trying ur solution.

It gives me following error.

script.sh[6]: full - new: 0403-009 The specified number is not valid for this command.
xargs: 0402-057 The /usr/bin/echo command was not found or could not be run.


And could u pls describe ur code.I would appreciate that.


Regards,
raj.
# 7  
Old 12-09-2010
Quote:
Originally Posted by rajsharma
Hi ROHON,
And could u pls describe ur code.I would appreciate that.
Regards,
raj.
You can give a try to the above Awk script i posted!!!Smilie
This User Gave Thanks to malcomex999 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk or sed? rows text to co

Hello Friends! I would like to help the masters ... I have a file with the entry below and would like a script for that output: Input file: 001 1 01-20152142711532-24S 1637909825/05/2015BAHIA SERVICOS R F, ... (1 Reply)
Discussion started by: He2
1 Replies

2. Shell Programming and Scripting

Indexing each repeating pattern of rows in a column using awk/sed

Hello All, I have data like this in a column. 0 1 2 3 0 3 4 5 6 0 1 2 3 etc. where 0 identifies the start of a pattern in my data. So I need the output like below using either awk/sed. 0 1 (2 Replies)
Discussion started by: ks_reddy
2 Replies

3. Shell Programming and Scripting

awk for string manupulation

Hi All, I have one file with two columns separated by tab. I need to search for second column value of this file in the 5 column of another file. If the match is found replace the 5th column of second file with entire row of the first file. e.g. file1 123 D.abc 234 D.rde 4563 ... (2 Replies)
Discussion started by: alok2082
2 Replies

4. Shell Programming and Scripting

Awk/sed script for transposing any number of rows with header row

Greetings! I have been trying to find out a way to take a CSV file with a large number of rows, and a very large number of columns (in the thousands) and convert the rows to a single column of data, where the first row is a header representing the attribute name and the subsequent series of... (3 Replies)
Discussion started by: tntelle
3 Replies

5. Shell Programming and Scripting

CDR manupulation

Hello Friends, I need to examine a huge CDR file according to a complex (at least for me) condition like below and i couldnt write anything :( In CDR file there are more than hundreds of fields, I need to print the rows which matches the below condition: while $13 field of subsequent... (9 Replies)
Discussion started by: EAGL€
9 Replies

6. Shell Programming and Scripting

Using sed (or awk or perl) to delete rows in a file

I have a Unix file with 200,000 records, and need to remove all records from the file that have the character ‘I' in position 68 (68 bytes from the left). I have searched for similar problems and it appears that it would be possible with sed, awk or perl but I do not know enough about any of these... (7 Replies)
Discussion started by: joddo
7 Replies

7. Shell Programming and Scripting

Combining multiple rows in single row based on certain condition using awk or sed

Hi, I'm using AIX(ksh shell). > cat temp.txt "a","b",0 "c",bc",0 "a1","b1",0 "cc","cb",1 "cc","b2",1 "bb","bc",2 I want the output as: "a","b","c","bc","a1","b1" "cc","cb","cc","b2" "bb","bc" I want to combine multiple lines into single line where third column is same. Is... (1 Reply)
Discussion started by: samuelray
1 Replies

8. Shell Programming and Scripting

get text between two special rows ?(awk or sed)?

Hello, I've the follwing text: gfdgfg -------------------------------- dfgfdgdfg fdgfdgfdgdgf fdgf ------------------------------ f g gdgf a constant string that i know --------------------------------------------- data I want to have data I want to have data I want to have data I... (16 Replies)
Discussion started by: eric_
16 Replies

9. Shell Programming and Scripting

Get text between two special rows ?( using awk or sed)?

Hi Friends I am facing some problem in extract the lines between two fixed lines for examplemy text file look like ... -------- line 1 line 2 line 3 --------- line 4 line 5 -------- line 6 line 7 line 8 line 9 line 10 --------- now i want the data between "-------" these... (4 Replies)
Discussion started by: sushantnirwan
4 Replies

10. Shell Programming and Scripting

sed or awk to convert text files with recurring headings to rows and colum

I have many text file reports generated by a Information Assurance tool that I need to get into a .CSV format or Excel tab delimited format. I want to use sed or awk to grab all the information in the sample text file below and create column headings:Risk ID, Risk Level, Category, Description, How... (5 Replies)
Discussion started by: Bjoeboo
5 Replies
Login or Register to Ask a Question