Help with awk, where line length and field position are variable
I have several questions about using awk. I'm hoping someone could lend me a hand. (I'm also hoping that my questions make sense.)
I have a file that contains pipe separated data. Each line has similar data but the number of fields and the field position on each line is variable.
Using the example above, I need to extract specific data from the following fields. (Shown below.) I'm struggling because the data on each line could be in a different position.
Running a command like this works on the first line but not the second.
What's the best way for me to extract data for these fields?
Next, how can I print everything from x-field to the end of the line, when the number of fields is unknown (fields 9, 10 and 11, as an example). I'm sure there's a better way to get the last three fields, for example, without enumerating each field.
The following awk command gives me everything from $6 on but will also print $1-$5 again.
Lastly, is there a way to print part of a field, followed by other fields? Using the example above, I'd like to print the following:
How can I cut the last four digits from the first field and continue to print the rest of the fields?
the NF variable is the number of fields in current record. You seem to require $1"|"$2"|"$3"|"$4"|"$5"|"$(NF -4)"|"$(NF -3)"|"$(NF -1)"|"$(NF -1)"|"$NF
The awk below produces the current output, which will add +1 to $3. However, I am trying to add the length of the matching characters between $5 and $6 to $3. I have tried using sub as a variable to store the length but am not able to do so correctly. I added comments to each line and the... (4 Replies)
The awk below using the sample input would output the following: Basically, it averages the text in $5 that matches if $7 < 30 .
awk '{if(len==0){last=$5;total=$7;len=1;getline}if($5!=last){printf("%s\t%f\n", last,... (6 Replies)
Hello,
I have a file with a date field with various lengths. For example:
m/d/yyyy hh:mm or h:mm
mm/dd/yyyy hh:mm or h:mm
Is there a way using sed or awk to change the field to m/d/y ? I don't need the hours and minutes in that field, just the date in the proper format.
Thanks in... (6 Replies)
Hi gurus,
I am trying to figure out how to extract substring from file line (all lines in file), as specified position and specified legth.
Example input (file lines)
dhaskjdsa dsadhkjsa dhsakjdsad hsadkjh
dsahjdksahdsad sahkjd sahdkjsahd sajkdh adhjsak
I want to extract substring on... (5 Replies)
Hello Everyone,
I am stuck with one issue while working on abstract flat file which i have to use as input and load data to table.
Input Data-
------ ------------------------ ---- -----------------
WFI001 Xxxxxx Control Work Item A Number of Records
------ ------------------------... (5 Replies)
I am trying to get text from a webpage, in terminal form. So far I am:
1. getting the html for the page printed using curl (curl -s webpage.com), which is then
2. piped to awk, printing line number 29 (awk NR==29), then
3. this is where I am sort of lost. I know where in the printed line I... (7 Replies)
Hi Unix experts
I have simple text files in which the number of lines vary from one file to another. They look like the following:
#
#
.
.
34 46
76 72
39 68
I want to grab the first number of the last line of each file (let's say A= 39 in the above example), which is... (2 Replies)
To trim 3rd field in for all the lines of a file and replace the modified string in that particular field.
For example i have a file called Temp.txt having content
Temp.txt
-----------------
100,234,M1234
400,234,K1734
300,345,T3456
----------------
So the modified file output should... (4 Replies)