LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   quick question (https://www.linuxquestions.org/questions/programming-9/quick-question-415026/)

0aniel 02-13-2006 02:49 PM

quick question
 
Hi,

Just would like to know,

How can you get awk to extract first 8 characters, then field $2 with 6 lines into a new file?

Or how can awk print $2 with only first 6 lines from that field?

Tinkster 02-13-2006 03:56 PM

Eh?

I think you need to elaborate on what constitutes a field for you :}

Or, best of, give an example of what you have and what you'd like to get.


Cheers,
Tink

0aniel 02-13-2006 04:54 PM

I have a field like the one below;

1
2
3
4
5
6

I want to extract the '6' entry into a new file.

Tinkster 02-13-2006 05:01 PM

I still don't understand ... to me that's six lines, where's the
field? And, what constitutes a record if 6 is a field?


Cheers,
Tink

0aniel 02-13-2006 05:10 PM

Sorry,

Well

Lets say this is my file
$1 $2
01 10
02 20
03 30
04 40
05 50
06 60

Idealy What I want to do is add all the lines together in $2 and extract the total into a new file.

Tinkster 02-13-2006 05:14 PM

Quote:

Originally Posted by 0aniel
Sorry,

Well

Lets say this is my file
$1 $2
01 10
02 20
03 30
04 40
05 50
06 60

Idealy What I want to do is add all the lines together in $2 and extract the total into a new file.

So the result would be what?
Code:

$1 $2
 01 01
 02 03
 03 06
 04 10
 05 15
 06 16

I still don't understand.


Cheers,
Tink

chrism01 02-13-2006 05:20 PM

I think he means add all the fields in column $2 ... or not ... ;)

0aniel 02-13-2006 05:25 PM

Yes add all the fields in column $2

10+20+30 etc and extract the total into new file..

Tinkster 02-13-2006 05:32 PM

awk '{ if(NR <7){ sum+=$2 }}END{ print sum }' file > result

And the correct (understandable) question would have been:
"How can I add the values of field 2 for the first six
lines of a file and save the result to a separate file?"





Cheers,
Tink

0aniel 02-13-2006 05:40 PM

Cheers Tink

0aniel 02-13-2006 05:57 PM

Ok lets say, the format of the file is exactly like the one below. My question would be how can you do the same process as above but all results for the first six lines are saved into same separate file?

$2
10
20
30
40
50
60

10
20
30
40
50
60


20
30
40
50
60
70

So the output of the result file would look something like;
180
180
240

Tinkster 02-13-2006 06:38 PM

Every file can only have one set of "first 6 lines" ;} ...

You're talking about records this time, and the records
are separated by one or more empty lines.

man awk
/RS
is a good starting point, you can use what I provided above as
a starting point. :}


Cheers,
Tink

0aniel 02-13-2006 06:43 PM

Yeh records are separated by one empty line. And they are always 6 lines long. As specified in previous post.

0aniel 02-13-2006 07:01 PM

I have looked at the awk man, there aren't any specific examples that I can relate to? So not sure how to preform it.

Tinkster 02-13-2006 07:37 PM

Code:

awk 'BEGIN{RS="";FS="\n";OFS="\n\n"} ...
to begin with ...


Cheers,
Tink


All times are GMT -5. The time now is 04:45 AM.