awk - pattern-directed scanning and processing language

learn more… | top users | synonyms

1
vote
2answers
51 views

Script to hang up Asterisk channels

I am trying to create a script for Asterisk. We use a lot of channspy. Sometimes the channspy stays opened and didn't hangup even if the softphone is hangup (I don't understand why). So I thought of ...
4
votes
3answers
156 views

Get last part of string after hyphen

Is there a simple command line to extract the last part of a string separated by hyphens? E.g., I want to extract 123 from foo-bar-123.
2
votes
1answer
51 views

How to select lines and reformat command output?

Is there a way to format the following output so that only lines 1 and 4 print out? It would be best if the first line and fourth line could come out on the same line like this: sw pool test(no ...
1
vote
5answers
59 views

keep out line break

I have some files with this structure: 2015-03-25 17:08:17 sysUpTimeInstance 93474;^M 1.ValueforState=2500 I want to replace the line break, and leave the third line with the second line, I mean, ...
-2
votes
1answer
47 views

how to print new word after two lines using awk

I need to insert a blank line after two lines in a file that i did with cat rat hat pot NOT lot big big big sit sit sit awk '{print};(NR%2==0){print ""}' cat rat hat pot NOT ...
1
vote
1answer
50 views

sort and uniq in awk

I know there are "sort" and "uniq" out there, however, today's question is about how to utilise AWK to do that kind of a job. Say if I have a list of anything really (ips, names, or numbers) and I ...
1
vote
1answer
35 views

awk script to rearrange similar rows

I want rearrange about 5 million rows (with 300 columns) into groups. Data looks like the following: where there were various experiments (column 2) conducted at different locations (column headers ...
0
votes
4answers
42 views

AWK print regex pattern

I am trying to print a regex pattern for the following piece of mail log. In particular I am trying to get the ID between the square brackets (see the second line for reference). Mar 29 03:48:13 ...
0
votes
3answers
39 views

How to display only the last line of a listing ?

Supposed I make a listing and sort the files by its temporal attribute: ls -ltr -rwxrwxrwx 1 bla bla 4096 Feb 01 20:10 foo1 -rwxrwxrwx 1 bla bla 4096 Feb 01 20:12 foo2 . . . -rwxrwxrwx 1 bla bla ...
0
votes
2answers
63 views

Print awk's current new line character

How can I find what new line character is in my file when using awk? I know the default is \n but this is what manual says. I want to see it with my own eyes. I have just started learning awk and ...
1
vote
1answer
48 views

Write text on last line of document [duplicate]

If I have this document aaa bbb ccc And I want to add " ddd" on the last line. My document should look like this: aaa bbb ccc ddd How can I do this? If I prefer awk or sed but ...
2
votes
2answers
88 views

How to print from a given range of ip addresses every class-c with the last octect replaced by an asterisk

example: I have a range from 10.0.100.0 to 10.0.224.0 and I want the output like this: 10.0.100.* 10.0.101.* 10.0.102.* and so on
-1
votes
1answer
31 views

AWK and Bash Scripting [duplicate]

How to use a value of a variable in awk? Something like this: filename = "test.txt" ls -l | awk '{ if ($9 == filename) print("File exists")}' I can't use $ in awk to access the value of ...
5
votes
1answer
57 views

How to use ^#$ as record separator in awk?

How do you tell awk to use a # character by itself in a line as record separator? The problem is you can't say RS="^#$" because ^ matches the beginning of the file, not the beginning of a line, and ...
0
votes
2answers
98 views

How to delete specific lines within a certain area?

I want to delete some specific lines in the YAML front matter: --- Author: aaaa Title: bbbb Time: cccc Place: dddd ... Lets say I wanted to delete only the lines in the front matter that contained ...
3
votes
2answers
144 views

Nested 'awk' in a 'while' loop, parse two files line by line and compare column values

I need some help with a combination of awk & while loop. I have two simple files with columns (normal ones are very large), one representing simple intervals for an ID=10(of coding ...
0
votes
2answers
75 views

sed command to replace a string from one file with entire contents of another file [duplicate]

I want to write a sed (or awk) command to replace a string from one file with the entire contents of another file. Note that second file from which I want to get the content has more than one line. I ...
1
vote
1answer
59 views

awk with variables in condition and in output redirection file

I would please like some help with this command because I didn't find anything in documentation that can cover everything I want. I have some variables that are global, so I would prefer to keep them ...
4
votes
3answers
144 views

sum columns based on matching fields

I have encountered a problem that exceeds my basic unix knowledge and would really appreciate some help. I have a large file in the following format: 2 1019 0 12 2 1019 3 0 2 1021 0 2 ...
1
vote
1answer
56 views

awk pattern with variable in a bash script

I have problem with command awk in bash I want to find pattern with variable, but it doesn't work. Can you tell me what's wrong with this line? awk -F" " "/$PWD/ {print $1,$3}" file.txt
1
vote
3answers
137 views

how to delete line from the XML file

how to delete only the line from xml file ( with sed/awk or perl one liner line ) that start with: <units> and ended with </Networks> as the follwoing <units><unit ip= ...
1
vote
4answers
120 views

bash division of numbers in text file

I have two numbers in test.txt file, I would like a way to divide the second number by the first number and assign the result to a variable or file in a bash script that I can use. I have used awk ...
1
vote
2answers
54 views

How to read only the second last line of a file

I have a client that updates/writes into myfile.csv arbitrarily. I've written the following code, I left the MySQL snippet out: while true do awk_variables_value_array=`tail -n2 myfile.csv | awk ...
1
vote
1answer
27 views

awk adds extra comma at several places

i want to add a timestamp with awk to a CSV-file. To generate the timestamp works, but the result CSV-file have some extra comma at several places. How can I fix the problem? origin-CSV (1st line): ...
1
vote
1answer
104 views

awk directory of files

Given a directory full of .sam files, for each file: if column3=blah and 451000 =< column4 <= 468999, write line to file_ribos.sam otherwise, write line to file_non_ribos.sam Example Input ...
1
vote
1answer
19 views

Awk - matching on 2 columns for differents lines

Given this file: 92157768877;Sof_deme_Fort_Email_am_%yyyy%%mm%%dd%;EMAIL;20/02/2015;1;0;0 92157768877;Sof_trav_Fort_Email_am_%yyyy%%mm%%dd%;EMAIL;20/02/2015;1;0;0 ...
1
vote
1answer
59 views

Awk sum csv columns based on fields

I have N files like so: file1.txt Header1,Header2,Header3,Header4,Header5 A,B,RANDOM,1,2 C,D,RANDOM,3,4 fileN.txt Header1,Header2,Header3,Header4,Header5 A,B,RANDOM,1,2 C,D,RANDOM,3,4 They all ...
2
votes
5answers
180 views

How to reverse the lines between a pattern into another file

I have the following in the input file #Start para 0 hello dq 1 world dq 2 welcomes dq 3 you dq #Start para 0 how tq 1 are tq 2 you tq #Start para 0 say dq 1 hello dq 2 to dq 3 the dq 4 world dq I ...
1
vote
2answers
39 views

How do I update the dates (by 30 days) in the files going to that specific location in unix?

How do I update the dates (by 30 days) in the files going to that specific location in unix? Can awk or sed help ? File format is as below - Thomson: xyz Format: 1 Character_set: ASCII8 File_type: ...
0
votes
1answer
43 views

Using “case” with awk

Some time ago, I used this part of my awk script: case "AlarmLevel.0" : alarmAlertLevel=$2 break But now, the version of information has changed, and the level is dynamic, I mean now, the ...
2
votes
2answers
34 views

How to Save variables in a script that can be shared between two runs of awk against the same input file in the script? [closed]

I have a log file, whose formatting is very consistent and which I control. It produces pipe-delimited fields of constant length. Two fields are relevant to the filtering process I wish to create, ...
3
votes
3answers
152 views

Splitting a sequence into fixed width

I have a file like this which is a two-column tab-separated file. CTGCAGTTTCCCCAAATGTGGGAAACTTGACTGTATAATTTGTGGCAGTGGTA a1 GATTTCCCCAAATGTGGGAAACTCACTCGGCAGGCGTTGATA a2 I want to get an output ...
1
vote
3answers
45 views

How to do find and replace in one field based on the value in the other field?

I have a file like this. IMS00|20140101|Unix|Jan14 IMS01|20140201|Unix|Feb14 IMS02|20140301|Unix|Mar14 IMS03|20140101|Unix|Jan14 IMS04|20140201|Unix|Feb14 My criteria are, if the column 1 value is ...
2
votes
1answer
25 views

AWK get first line

I'm trying to add a column for each line together, and I do it like this: awk '{s+=$2} END {print s}' Now, when I have the final sum, s, I want to subtract the second column of the first line, like ...
0
votes
1answer
95 views

/usr/bin/awk: Argument list too long

I am trying to run the following bash script: #!/bin/bash file=$1 filename=${file%%.*} line1=$(sed -n 1~2p ${file}) seqs=$(grep -v '^>' ${file}) pos=$(echo "${line1}" | awk -F"[__]" ...
2
votes
1answer
79 views

How to add number to awk array elements?

If I have the following bash variable: $ echo "${pos}" 201 719 744 205 354 ...the following produces... !#bin/bash ( IFS=: awk -v str2="$pos" -v sep="[$IFS]" ' BEGIN { ...
-3
votes
1answer
68 views

Using AWK to define variables in a shell

I've got a live MQTT stream being filtered out and dumped into a CSV file. I use the fourth parameter ($4) and fifth parameter ($5) in the CSV file to locate the information I want. I give this ...
1
vote
3answers
107 views

Append based on line count in awk after pattern match

How can I append lines after a pattern match in awk based on how many lines they are after the pattern match. What I have looks like this: Stuff PATTERN a b c a b c a b c ENDOFSECTION Morestuff I ...
1
vote
2answers
54 views

Bash: How to create an alias in .bashrc for awk with parameters

I'm trying to add an alias in .bashrc file as follows: ... alias cutf="_cutf" ... _cutf() { awk 'NR >= $2 && NR <= $3 { print }' < $1 } (The function's goal is to show the ...
1
vote
1answer
44 views

How to use grep/awk/unix to match all lines from one file in another file, even if they are duplicates

I'm trying to retrieve the the matching lines from a file, however the file that has the "index" or list of matches to retrieve also has duplicate entries and I want to print out the duplicates as ...
0
votes
2answers
68 views

Grep for a word within two inverted commas

How to grep for a word within two inverted commas. The word may contain any number of characters . e.g "two" , "three"
0
votes
1answer
47 views

Parse header in a file and based on the header replace a value in the file

I have a problem I need help with. I'm not very experienced with awk, but it seems to be a good tool for this job. I have the following file Field1|Field2|Field3|Field4|Field5 a|b|c|d|e 1|2|3|4|5 ...
4
votes
2answers
76 views

How to cut from the 1st character to -7 character in awk?

I have a file with multiple lines. Which the second column is the user_id, but the last seven number are random numbers. It need to be trimmed off to get the authentic ID. So what I need is to get ...
1
vote
3answers
42 views

How to can I make Filename Sequencing with the help of Loops

my intention is to keep the code as simple as possible.Here there are 5 files namely A_1.CSV, A_2.CSV, A_3.CSV, A_4.CSV, A_5.CSV The below code retrieves the first row in a CSV file. Code : ...
1
vote
1answer
36 views

How to print incremental count of occurrences of unique values in column 1

I'm trying to come up with an solution to this problem, I need to incrementally count and then print the counts of the unique values in column 1 of a tab delimited text file. Here is an example: ...
0
votes
3answers
44 views

I want to condense a list of ranges (from-to)

I have data involving codes assigned to ranges in the form of From To Code 0 5 A 5 10 A 10 15 A 15 20 B 20 25 B 25 30 A 30 35 A 35 ...
0
votes
1answer
58 views

Avoid conversion to scientific notation in awk

I have a csv file that contains hundreds of columns. I would like to subtract the value of a specific column from a static number and then print the entire line. The values of that column are long ...
1
vote
2answers
42 views

Appending an input file with awk after a pattern (looping?)

I am trying to take a text file called 'input', of the form line1 line2 line3 PATTERN x y z x y z lineN and use awk to place a number (1) after each "x y z" line. I need the lines before the ...
1
vote
1answer
101 views

remove lines where a field's value is less than or equal to 3 - sed or awk?

I need to remove every line that has a value of 2 or less in the 8th field (column). My data looks like this: 12-31 Airport 189 379 41 49.70946503 -124.91377258 2 2880 30.8 01-01 ...
3
votes
5answers
183 views

Pick the latest file based upon the sequence number given in the file name

I have a requirement, I have few files in a directory, I need to pick the oldest one for processing. For Exp: I have below files in /tmp directory: Sample_0000237826-001_xyz.dat ...