awk

creating tidy shell script from ugly command line pipeline

Hi I wrote a piped shell command that has multiple pipes in it that works great. I now want to put this in the form of a (tidy) shell script. Here is the script: #!/bin/bash for number in `cat xmlEventLog_2010-03-23T* | sed -nr "/<event eventTimestamp/,/<\/event>/ {/event /{s/^.*$/\n/; p};/payloadType / {h; /protocol/ {s/.*protocol=\"(...

How to Awk Paragraph in complex text file?

Hi there! I have a simple log file which is very messy and I need it to be neat. The file contains log headers but are all jumbled up together therefore I need to sort the log files according to the log headers. There are no static number of lines that means that there is no fix number of lines for the each header of the text file. And I...

awk return parent HTML tag value if its child tag content is matched - possible?

Hello, I've been searching for solution to this problem for quite some time, but I can't figure it out on my own. So I have bunch of HTML blocks of code, and I want to search for specific string that is contained in one of the inner tags and if there's match I want return it's parent tag value. Here's example" <li rel="Returns this val...

How to pass AWK output into variable?

Hi there! I have a small bash script that greps/awk paragraph by using a keyword. But after adding in the extra codes : set var = "(......)" it only prints a blank line and not the paragraph. So I would like to ask if anyone knows how to properly pass the awk output into a variable for outputting? My codes: #!/bin/sh set var = "(awk...

Coloring/indenting a script within script in Emacs

I often have shell scripts that call other scripting languages, like so: #!/bin/bash cat somefile|awk ' BEGIN { #This line is not auto-indented and is colored as a string constant. ... } { #Same with this line. ... }' echo "More Bash code here." ... Is there a way to make Emacs recognize the awk string as an awk program in...

How to express "map a linux command to each line in a file"?

Often I need to delete all the files not in a specific svn source tree. To get the list of all their file names, I use: svn st | grep ^? | awk '{print $2}' This command will give me a list of file names, one name per line. Then how can I express the idea of for (each line in ${svn st | grep ^? | awk '{print $2}' ) rm -f line ? ...

bash: getting percentage from a frequency table

i had made a small bash script in order to get the frequency of items in a certain column of a file. The output would be sth like this A 30 B 25 C 20 D 15 E 10 the command i used inside the script is like this cut -f $1 $2| sort | uniq -c | sort -r -k1,1 -n | awk '{printf "%-20s %-15d\n", $2,$1}' how can i mod...

Convert parts=path1,size1,...,lastPath,-1 :-> parts=nPath,nSize,lastPath,-1 ; all possible conditions (no path1,size1,... ; OR no lastPath,-1 )

i have following pattern parts=/a,1mb,/b/c,2gb,/zee/last,-1 #general form on next line ^parts=path1,size1,...,lastPath,-1$ I want to replace all $path1,$size1 except $lastplace,-1 with $newPath,$newSize i.e. parts_new=/p,2mb,/q/r,5gb,/zee/last,-1 #general on next line ^parts=newPathX,newSizeX,lastPath,-1$ I figured how to do it usi...

return common fields in two files

Hi, I have two files, should compare 1st column of file1 with 1st column of file2 and the resultant file should be file2 For example: file1 apple banana Mango potato tomato file 2 apple:fruit brinjal: vegetable lady's finger: vegetable orange: fruit tomato: vegetable potato: vegetable Resultant file should look something like th...

Mapping a string to a unique number?

Is there a nice bash one liner to map strings inside a file to a unique number? For instance, a a b b c c should be converted into 1 1 2 2 3 3 I am currently implementing it in C++ but a bash one-liner would be great. ...

Bash: sort text file by last field value

Hi All, I have a text file containing ~300k rows. Each row has a varying number of comma-delimited fields, the last of which is guaranteed numerical. I want to sort the file by this last numerical field. I can't do: sort -t, -n -k 2 file.in > file.out as the number of fields in each row is not constant. I think sed, awk maybe the ans...

Assigning a variable on each line in a quick awk command

Awk is awesome of text manipulation, but a little opaque to me. I would like to run an awk command that boils down to something like this awk '{$x = ($3 > 0 ? 1 : -1); print $1*$x "\t" $2*$x}' file I want to assign $x on each line, i.e. not using the -v option, and then use it inside my print statement. Unforunately, after the ; awk...

Bash script using awk in for loop

for i in $LIST do CFG=`ssh $i "cat log.txt|awk '{print $2}'"` for j in $CFG do echo $j done done Say I want to print 2nd field in the log file on a couple remote host. In above script, print $2 doesn't work. How can I fix this? Thanks. ...

What's the best way to convert "awk '{print $2 >> $1}' file" in a Perl one-liner?

How could I convert: awk '{print $2 >> $1}' file in a short Perl one-liner? "file" could look like this: fruit banana vegetable beetroot vegetable carrot mushroom chanterelle fruit apple ...

How to separate words in a "sentence" with spaces?

Background Looking to automate creating Domains in JasperServer. Domains are a "view" of data for creating ad hoc reports. The names of the columns must be presented to the user in a human readable fashion. Problem There are over 2,000 possible pieces of data from which the organization could theoretically want to include on a report....

decompress name

what is the easiest way to decompress a data name? for exmaple change compressed form: abc[3:0] into decompressed form: abc[3] abc[2] abc[1] abc[0] preferable 1 liner :) ...

How to add a new column in a file

I have file like this which contains 5 columns. But some rows in my file have 4 columns by mistake, for example in this file at the 5th and 6th rows, you can see that the second column is missing. I want to replace the missing 2nd column with a blank space without disturbing other rows in my file with field separator ",". 11111,5323,62...

awk extract "Matthew" from mtjoseph:6/MKTCzMS/YU.:10974:10060:Matthew

How to extract "Matthew" from "mtjoseph:6/MKTCzMS/YU.:10974:10060:Matthew" ...

awk and sed challenge: adjust the text width

So let's see how can we do this: trim the text width within a certain value, say, 10. For lines longer than 10, break it into multiple lines. Example: A text file: 01234567 01234567890123456789abcd 0123 should be changed to: 01234567 0123456789 0123456789 abcd 0123 So how can we do it using sed or awk as short as possible? ...

SAG challenge (sed, awk, grep): multi patterns file filtering

So my dear SOers, Let me be direct to the point: specification: filter a text file using pairs of patterns. Example: if we have a file: line 1 blabla line 2 more blabla line 3 **PAT1a** blabla line 4 blabla line 5 **PAT1b** blabla line 6 blabla line 7 **PAT2a** blabla line 8 blabla line 9 **PAT2b** blabla line 10 **PAT3a** blabla line 1...