awk

in place editing using awk

I want to add a line at top of file say f1 using awk Is there a better way than : awk 'BEGIN{print "word"};{print $0}' f1 > aux;cp aux f1;\rm aux does awk has something like -i option in sed. ...

How to do a script in BASH which takes random text from file?

I have file like: aaa bbb ccc ddd eee And I want to do a script in BASH which can takes random line of this text file, and return it to me as variable or something. I hear it can be done with some AWK. Any ideas? UPDATE: I now using this: shuf -n 1 text.txt Thanks you all for help! ...

How can I count unique terms in a plaintext file case-insensitively?

This can be in any high-level language that is likely to be available on a typical unix-like system (Python, Perl, awk, standard unix utils {sort, uniq}, etc). Hopefully it's fast enough to report the total number of unique terms for a 2MB text file. I only need this for quick sanity-checking, so it doesn't need to be well-engineered. ...

Perform action on line range in sed/awk

How can I extract certain variables from a specific range of lines in sed/awk? Example: I want to exctract the host and port from .tnsnames.ora from this section that starts at line 105. DB_CONNECTION= (description= (address= (protocol=tcp) (host=127.0.0.1) (port=1234) ) (connect_data= ...

how to split lines in unix log file output

Hello, I'd like to be able to parse date and times out of a log file. Currently they're in the following format: "02/Jun/2009:14:38:50" but i'd like to separate them in different columns using something available from a linux command line so that the resulting output looks as follows: "02/Jun/2009" "14:38:50" could someone please sh...

Split remaining fields in sed/awk

I have the line: MAVEN_OPTS=-XX:MaxPermSize=128m -Xms128m -Xmx768m and want to convert it to (add quotation marks on field 2->n): export MAVEN_OPTS="-XX:MaxPermSize=128m -Xms128m -Xmx768m" ...

Ever need to parse the svn log for files committed by a particular user since a certain date?

If so the following one-liner utilizing awk might provide a useful template svn log -v -r{2009-05-21}:HEAD | awk '/^r[0-9]+ / {user=$3} /yms_web/ {if (user=="george") {print $2}}' | sort | uniq ...

awk insert blank line every n lines

Probably easy to answer for you guys: How can i get awk to put a blank line into my file every n lines? ...

Awk/etc.: Extract Matches from File

I have an HTML file and would like to extract the text between <li> and </li> tags. There are of course a million ways to do this, but I figured it would be useful to get more into the habit of doing this in simple shell commands: awk '/<li[^>]+><a[^>]+>([^>]+)<\/a>/m' cities.html The problem is, this prints everything whereas I simpl...

Can I chain multiple commands and make all of them take the same input from stdin?

In bash, is there a way to chain multiple commands, all taking the same input from stdin? That is, one command reads stdin, does some processing, writes the output to a file. The next command in the chain gets the same input as what the first command got. And so on. For example, consider a large text file to be split into multiple files...

How can I create directories during a copy in Zsh?

I get the following messages often, for instance when coping dev files to a master branch cp: /Users/Masi/gitHub/shells/zsh/dvorak: No such file or directory cp: /Users/Masi/gitHub/shells/zsh/dvorak2: No such file or directory I would like to be asked about the creation of the given folders such that my initial command will be run if ...

Forcing the order of output fields from cut command

I want to do something like this: cat abcd.txt | cut -f 2,1 and I want the order to be 2 and then 1 in the output. On the machine I am testing (FreeBSD 6), this is not happening (its printing in 1,2 order). Can you tell me how to do this? I know I can always write a shell script to do this reversing, but I am looking for something us...

Unable to increase AWK's int-limit for factorial

I run the AWK code and I get The factorial of 200 is inf This suggests me that AWK does not use the same int IEEE-standard -module as Python. It seems that AWK's limit is 170!. How can you make AWK understand as large integers as Python? ...

choose the newest file and use getline to read it

Hi! Having problems with a small awk script, Im trying to choose the newest of some log files and then use getline to read it. The problem is that it dosent work if I dont send it any input first to the script. This works echo | myprog.awk this do not myprog.awk myprog.awk BEGIN{ #find the newest file command="ls -alrt | tail -1...

How to delete a part of the file with awk

Hi I'm writing a shell script, which at some point has to take a file, search for a particular word in it and delete the whole text that comes after this word (including the word itself) - awk is the right tool I suppose, but I don't really know much about programming in it. Could anyone help me? ...

Looking for way to automate testing kshell app

I inherited a shell-script application that is a combination of kshell scripts, awk, and java programs. I have written JUnit tests for the java pieces. Is there a good way to do something similar for the kshell scripts and awk programs? I have considered using JUnit and System.exec() to call the scripts, but it seems like there shou...

Get a list of delimited filenames from a text file

Hi, I'm really new to Bash, so this could sound silly to most of you. I'm trying to get a list of some filenames from a text file. Tried to do this with sed and awk, but couldn't get it to work with my limited knowledge. This is a sample file content: <?xml version="1.0" encoding="utf-8"?> <!-- Generator: Adobe Illustrator 13.0.1, SVG ...

Using awk to remove the Byte-order mark

Hi, has anyone an idea how an awk script (presumably a one-liner) for removing a BOM would look like? Specification: print every line after the first (NR > 1) for the first line: If it starts with #FE #FF or #FF #FE, remove those and print the rest ...

awk help for getting fields

I have field like a_b_c_d I want as the output a b c_d, this query wont work for this awk -F"_" <file> | '{print $1,$2,$3}' Since it will only print a b c ...

How can I search for simple if statements in C source code?

I'd like to do a search for simple if statements in a collection of C source files. These are statements of the form: if (condition) statement; Any amount of white space or other sequences (e.g. "} else ") might appear on the same line before the if. Comments may appear between the "if (condition)" and "statement;". I want to ex...