AWK is a text-processing language. It is mainly used to manipulate and process structured data, and to generate reports.

learn more… | top users | synonyms

3
votes
4answers
13k views

Print line X lines before/after found line

I want to search for a particular string in a certain file. If I found the string, I also want to print the line X lines before (or after) that line. Can this with grep or awk, or do I need a ...
1
vote
1answer
312 views

printing only first finded match

I've created a statement which is searching for specific informations in each line (in my case "AAA","BBB" or "CCC"), if there is no such information N/A is printed { k=0; for (i=1;i<=NF;i++) ...
1
vote
1answer
30 views

awk is limited to only one line operations?

Like in the subject. Let say I have a file which contains: *aaa A B C D E F 55 *ccc A B C R E T 33 and I would know if it's possibe to make a statement if $1=="*aaa" then print $4 ...
0
votes
1answer
192 views

Find number in string and replace it

I wrote C++ code and now I want to replace the number in a string by using shell scripts. The part that needs to be replaced: if (random<=90) How I can change the 90 from a shell script? I ...
1
vote
1answer
74 views

AWK does not work correctly in my system [duplicate]

i cant run this code because of an error awk 'BEGIN {x=0;y=0} { match($13,/([0-9]+)/,a); x = x + a[1]; match($12,/([0-9]+)/,b); y = y + b[1]} END {print "rxPackets:" x " txPackets:" y}' yourfile ...
0
votes
2answers
125 views

sum numbers in different lines in shell

i have 8000 lines similar to this <Flow flowId="1" timeFirstTxPacket="+0.0ns" timeFirstRxPacket="+924100.0ns" timeLastTxPacket="+199984927558.0ns" timeLastRxPacket="+199675473275.0ns" ...
-1
votes
3answers
380 views

convert text file column field with data dd.mm.yy to dd.mm.YYYY

How to format date for file content using linux command line tools? Input: aaa;1.70;ccc;20.01.13;zz;2013; uuu;2.70;lll;17.12.10;zz;2013; fff;3.70;nnn;31.01.98;zz;2013; auu;8.70;nuu;30.02.96;zz;2013; ...
1
vote
1answer
27 views

`nawk` “doesn't work” with `make`? Why?

I have the following alias: alias mca_color='nawk '"'"'BEGIN { arr["Creating shared object"] = "1;31";} \ { l = $0; for (pattern in arr) \ { gsub(".*" pattern ".*", "\033[" arr[pattern] ...
2
votes
2answers
147 views

Use tail directly on website data, or another utility needed?

I am planning to extract a single field of data from the last line of data on a website to populate an RRD database. I'm running Linux. The website is: ...
1
vote
1answer
50 views

Precising if statement in awk

Is that possible to precise conditions in "if" statement? Let's take an example from my previous post (by the way, "k" variable is tricky, thanks terdon;)) and say that I'm looking for $i which starts ...
0
votes
2answers
246 views

combining loop “for” with statement “if”

I've created a script for awk. I'm combining loop "for" with "if" statement. It's searching each line for word "ABC" which can occure in different place in the line. It looks like below: {for ...
0
votes
0answers
41 views

several awk commands consecutively

I'm using awk command to extract some datas. The point is that I need to use awk command several times to extract all informations I need and each time when I'm using awk I need create a temp file. My ...
0
votes
1answer
79 views

combining bourne shell and awk

This is my actual file WASfile #!/bin/sh sed -i '/^ *$/d' WASfile sed -i -e '/user=/,/group_1=/{w /tmp/1' -e 'd}' /home/wasdm/WASfile; Script to Add read -p "Press [Enter] to continue for ...
0
votes
1answer
128 views

match patterns update output file uncomment when desired

Need suggestion for following. Have two files myfile and responsefile. First file myfile.txt user=myname user_1=yourname group=mygroup group_1=yourgroup second file responsefile.txt ...
0
votes
2answers
280 views

split a textfile after each n matches to a new file using sed or awk

i tried to split a file in parts of n matches each. The file is just one line and the seperator is '<br>' foo<br>bar<br>.....<br> I just want to split the file in parts, ...
0
votes
1answer
146 views

Why is this awk statement not working?

awk 'BEGIN { COLM_FMT = "%-8s, %-8s, %-8s, %-8s, %-8s, %-8s, %-8s, %-8s, %-8s, %-8s, %-8s, %-8s, %-8s, %-8s,\n" } { printf COLM_FMT, ${totals[0]}, ${totals[1]}, ${totals[2]}, ...
0
votes
1answer
55 views

Move numbers in a filename to the start

I have several files in a folder, with names in the form: SOMETEXT1 number SOMETEXT2.mp3, or SOMETEXT number.mp3. I want to rename these to number SOMETEXT SOMETEXT2.mp3, or number SOMETEXT.mp3. Using ...
1
vote
3answers
2k views

Unix - Surround First Column of CSV with double quotes

I have data in the following format - 4,"abc" 8,"def" 9,"ghi" I want to surround the value of the first column by double quotes. "4","abc" "8","def" "9","ghi" How do I do that ?
0
votes
2answers
34 views

Count the number of lines in a piped input?

While running ps -eaf I see a list of running processes. Is it possible to count the number to lines using awk?
1
vote
0answers
68 views

Automatically Split a Huge TeX File Into Many Smaller Files

How would I automatically split the sections or chapters of a huge TeX file into many smaller TeX files? This somewhat works: awk -v RS='\\chapter{' '{print "\\chapter{"$0 > (NR".tex")}' ...
0
votes
4answers
240 views

Changing multiple lines in to single line with spaces in linux

I have following data with multiple lines username: joe empid: 1111 status: resigned username: tom emid: 1234 username: kate empid: 2222 status: resigned What I want is to get the data in to ...
2
votes
1answer
2k views

Round off the decimal value in awk

i want to round off the value 262.5 to 263 using awk i have tried the below echo "262.5" | awk '{printf "%.f\n", $1}' My expected answer is 263
1
vote
2answers
777 views

Sum a particular column and take the unique record of another using AWK only

I have an input file like the following: 19 | 17 | 1 | 89 | C | 9 | 100 | 9 | PL | 2175 20 | 17 | 1 | 89 | C | 9 | 100 | 9 | PL | 2175 21 | 17 | 2 | 89 | C | 9 | 100 | 9 | PL | 2175 23 | 17 | 2 | 89 ...
8
votes
6answers
13k views

How to get video duration in seconds?

How can I get video duration in seconds? What I've tried: ffmpeg -i file.flv 2>&1 | grep "Duration" Duration: 00:39:43.08, start: 0.040000, bitrate: 386 kb/s mediainfo file.flv | grep ...
0
votes
1answer
2k views

How can I find my awk version?

If I want to know the version of awk I get the following: $ awk --version awk: not an option: --version Checking in man awk I see that my awk is mawk - pattern scanning and text processing ...
1
vote
2answers
312 views

How can I sort and de-dupe a very long list of words?

I have a 400 megabyte file. The file is a list of words, each word on a single line. End of lines are a mix of cr/lf and lf. Some of the words are unicode. I'd prefer answers for Linux or Windows ...
0
votes
2answers
734 views

Attempting to read data from a text file and inputting the variable text to a script

I have a file with 25 single word entries, I need a script to read through the file one line at a time and input the variable phrase into a second script that will generate a individual file based on ...
0
votes
2answers
119 views

Trying to get example from Awk and Sed book to work on Ubuntu 13.04

I have been developing Windows Software for years. I am trying to branch out and learn Linux. It will really help with my new job. I have picked up the book, "Awk and Sed" 2nd Edition. I am ...
0
votes
1answer
253 views

Find a pattern in a line, and append that matched pattern to the same line

I'm trying to pull a job number out of notes listed in a csv, and then append that number to the end of the line, preferably through sed, awk, grep or perl (which I have installed in Cygwin. Here's a ...
1
vote
2answers
454 views

Change names in a column in bash

I have a tab-separated file in Linux that looks like this ~$ head list.tab "x" "1" "FUHGF.jnf.m22-1" "2" "HDFHFEY.gfs.d2-1" "3" "KJFGJF.fr.md2-1" "4" "SDFSDIB.gfd.rtl2-1" "5" "FGJFGOB.hgd.k2-5" "6" ...
0
votes
3answers
3k views

Better way to replace timestamp by date when reading a squid log

Tonight i was trying to look at my proxie's lines, and I came up with this, which seems all but KISS. How would you do it simply ? awk '{ trunc=sprintf("%.0f", $1) ;$1="" ; system("echo -n `date +%R ...
0
votes
1answer
64 views

Sed/Awk line double

I have a list like this. Bob Jim Steve Is there a way in bash to turn that into a list like this? <a href="http://example.com/Bob"> Bob <a href="http://example.com/Jim"> Jim <a ...
0
votes
1answer
83 views

trying to extract two tables at a time from http files with 40 tables each

I have about 20 webpages. Each page has top banner navigation and then has information on up to 20 vehicles. There are 2 tables per vehicle. The logical flow is: page navigation, table 1 for ...
3
votes
3answers
351 views

Need to sed awk or grep output to extract strings

I have output which looks like this: "vancouver/news/vancouver-voices": { "status": 0, "time": 0, "timestamp": 1371298701 }, "vancouver/news/vancouvers-march-second-annual-success": { ...
-2
votes
1answer
80 views

Grep word in one file, and use that word to match in another file, adding the thing that follows the match

I want to grep several words in file1, and use each word to grep what follows after its match in file2. And then I want to add the string that followed the match to the word I used into file03, so ...
1
vote
1answer
1k views

using sed/awk to parse XML tags

For the record, I've spent several days working on this with no luck. I'm working with XML files with data containing something like the following: <row id="67581917031" name="4022" ...
0
votes
3answers
8k views

remove text from file

I want to remove some text from file1.txt. I put the text in the file tmp and do: grep -f tmp file.txt But it gives me only the difference. The question is how to remove the difference from ...
1
vote
1answer
405 views

How to render two columns preserving all whitespace after the first column with awk or alternative?

my output comes in human-readable figures with one space followed by directory names: I am currently using ... awk '{ printf "%-20s %-40s\n", $1, $2 }' input 1G foo 1.5M foo baz 5K foo spaces in ...
0
votes
1answer
246 views

How to split file on basis of size without breaking last line of split file

I have a big file and want to split the file on the basis of size. For e.g. Main file: 10 Mb and then I want to split it into 2 mb parts. But the issue is if I use split -b it breaks the last line ...
1
vote
1answer
151 views

sed/awk: modify source file?

I'm trying to script some changes I need to make to a couple of config files. It's worth mentioning that I'm an expert with neither sed nor awk, though I'm far better with sed. Right now, I'm doing: ...
0
votes
1answer
260 views

Using awk or cut command to get CPU percentage

I am trying to execute a Linux command through a PHP script to get the CPU percentage for each core: … with cut: $output = null; passthru("mpstat -P ALL | grep 0 | tr -s ' ' | cut -d ' ' -f 4", ...
0
votes
1answer
167 views

redirect awk information to stdout

I am trying to gather information for CPU use for all the processors in a text file. mpstat -P ALL -u 1 | awk '{print $4}' > cpu.txt && mptat -P ALL -u 1 | awk '{print $4}' | tee cpu.txt ...
1
vote
1answer
437 views

netcat output into separate files using string delimiters

I want netcat to pipe the output of a tcp/ip connection to commands that examine the output and split it into distinct files separated by the strings "msg" and "/msg" from the output, something like ...
0
votes
1answer
157 views

Counting by number of occurrences in first column

I have an input file like this: ATTACK-RESPONSES id check returned root BACKDOOR ACKcmdC trojan scan BACKDOOR hack-a-tack attempt BACKDOOR WinCrash 1.0 Server Active ICMP Destination Unreachable ...
0
votes
2answers
266 views

Computing average on a list of values for each value

I have a huge file (50k lines) with the following format: 52370581 2116 17996781 4314 47818829 2584 61421045 2158 ... 25145813 2274 The first value can be present multiple times and I want to ...
4
votes
1answer
6k views

Using grep to remove lines from a file which contain a string from another file

I have a file containing words (one per line) such as Dog Fish Cat Shoes I have a secondary file in CSV format such as 1,shoes,red 2,apple,black 3,fog,blue I would like to use grep with ...
0
votes
1answer
198 views

Write output to $TMPDIR/$variable : file does not exist (should be created)

I have the following .sh script in which I am trying to do all major writing in $TMPDIR. However, when it gets to line 86, it returns the following error: ...
11
votes
3answers
4k views

sed: how to replace line if found or append to end of file if not found?

With a single input file that only contains comments (starting with #) and VARIABLE=value lines, is it possible to replace a value for a single variable if found and, otherwise, append the pair to the ...
0
votes
1answer
485 views

Mass migration of EXIF GPS data (a.k.a. Escape from iPhoto)

I am trying to move my library of photos (over 12000) out of iPhoto for use in Adobe Lightroom and would like to include the GPS data I've added to the photos using iPhoto's in-app feature (This data ...
-1
votes
2answers
1k views

Extract IP addresses in each line of file in shell

I have this input file in ubuntu: 146.14.142.96.17747 197.102.40.184.13748: 146.14.142.96.17747 197.102.40.184.13749: 146.14.142.96.17747 197.102.40.184.13750: 146.114.142.96.17747 ...