awk

Humanized dates with awk?

I have this awk script that runs through a file and counts every occurrence of a given date. The date format in the original file is the standard date format, like this: Thu Mar 5 16:46:15 EST 2009 I use awk to throw away the weekday, time, and timezone, and then do my counting by pumping the dates into an associative array with the date...

Simple way to insert line to particular position in file

I need to insert string to text file, for example, before first line from the end. STRINGS=`wc -l $OLDFILE \ | awk '{print $1-1}' \ | sed "s/$DOLLAR/+/g" \ | tr -d \\\n \ | sed "s/+$DOLLAR//" \ | bc` ADDFILE=$3 head -n $STRINGS $OLDFILE > $NEWFILE cat $ADDFILE >> $NEWFILE tail -n 1 $OLDFILE >> $NEWFILE Can you sugg...

awk extract multiple groups from each line

How do I perform action on all matching groups when the pattern matches multiple times in a line? To illustrate, I want to search for /Hello! (\d+)/ and use the numbers, for example, print them out or sum them, so for input abcHello! 200 300 Hello! Hello! 400z3 ads Hello! 0 If I decided to print them out, I'd expect the output of 20...

Print last 10 rows of specific columns using awk

Hi, I have the below awk command-line argument and it works aside from the fact it performs the print argument on the entire file (as expected). I would like it to just perform the formatting on the last 10 lines of the file (or any arbitrary number). Any suggestions are greatly appreciated, thanks! I know one solution would be to ...

Modification of date format within a text file

Hello all, I have some text files containing lines as follow : 07JAN01, -0.247297942769082E+07, -0.467133797284279E+07, 0.355810777473149E+07 07JAN02, -0.247297942405032E+07, -0.467133797586388E+07, 0.355810777517715E+07 07JAN03, -0.247297942584851E+07, -0.467133797727224E+07, 0.355810777627353E+07 . . . . . . . . I need to prod...

Are there Unix platforms where Perl is not installed by default?

I am in the process of answering a request for tender on a contract that requires a decent amount of text processing. The main trouble is that the customer wants to be able to run this on any UNIX (HPUX, Solaris, AIX, FreeBSD) or Linux (SLES, RHEL) platform, which may constrain what I use to do it. They don't want to make the installatio...

Extract text from hostname

Using OS X, I need a one line bash script to look at a client mac hostname like: 12345-BA-PreSchool-LT.local Where the first 5 digits are an asset serial number, and the hyphens separate a business unit code from a department name followed by something like 'LT' to denote a laptop. I guess I need to echo the hostname and use a combinat...

problem with array elements in awk not being stored

I've been using awk to process hourly weather data, storing 10 arrays with as much as 8784 data elements. If an array is incomplete, i.e., stops at 8250, etc., after the "END" command I fill the remaining array elements with the last available value for the array. However, when I then print out the complete arrays, I get 0's for the fil...

Using awk to "remove" unwanted text from CSV file

Hi, I have a CSV file like this: SUPPORT 07/30/2008-10:59:54 Eastern Daylight Time 123 07/03/2009-08:56:42 Eastern Daylight Time DC321 07/10/2009-20:16:56 Eastern Daylight Time where the date is an entire column. how can i remove the entire "Eastern Daylight Time" part of the string with awk? ...

need help removing time from a csv file

im trying to process a csv and make it easier for sorting, and i need to remove the time and the dash from it. the file has entries like this: James,07/20/2009-14:40:11 Steve,08/06/2006-02:34:37 John,11/03/2008-12:12:34 and parse it into this: James,07/20/2009 Steve,08/06/2006 John,11/03/2008 im guessing sed is the right tool for t...

print a special column in table

Given this table: Employee -- firstname lastname salary age I want to print the first name if it's started by "a" also i want to print the name of employee who has a salary more than 200$ and less than 1000$. ...

Linux command to replace string in LARGE file with another string

I have a huge SQL file that gets executed on the server. The dump is from my machine and in it there are a few settings relating to my machine. So basically, I want every occurance of "c://temp" to be replace by "//home//some//blah" How can this be done from the command line? ...

To have AWK's `-F, '{ print $1 }' in PHP

I run unsuccessfully $cookie = "[email protected],777"; $cookie_tripped = trim(",", $_COOKIE['login']); echo "Cookie: "$cookie_tripped[0]; It gives me Cookie: How can you do the following command in PHP? awk -F, '{ print $1 }' ...

Using awk with NSTask

How would I use this awk command: awk 'NR>1{print $1}' string-to-modify with NSTask? I already tried setting /usr/bin/awk as the launch path, 'NR>1{print $1}' as an argument, then the string to modify as another argument but all I get is this: /usr/bin/awk: syntax error at source line 1 context is ' <<< /usr/bi...

How do I view all ignored patterns set with svn:ignore recursively in an svn repository?

I see it is possible to view a list of properties set on every directory within an SVN repository using proplist and the -R flag (recursive) and -v flag (verbose): svn proplist -Rv This shows me all properties, such as svn:mime-type or svn:executable. I'm looking to filter this to just svn:ignore properties. I'm sure there is some way...

A regex for a class definition in java

The problem is as follows: There is a massive codebase in Java (hundreds of files in tens of packages) where we need the ability to add and remove the keyword strictfp in each class definition. I am planning to use either sed or awk to perform this substitution. However, I would like to avoid the word "class" in comments or elsewhere fr...

grab frequent username from 'last' command in unix - with exceptions

If I run this on OS X: last -10 | awk '{print $1}' I get: chop chop chop chrihopk reboot shutdown chop chop chop chrihopk How do I use sed or awk to obtain the most frequent user 'chop'? ADDITIONAL EDIT TO QUESTION: Our local admin account interferes with the results (username: support) and often we have a new starter on a clie...

Using awk to get next fields

I have this input file (space is the separator for the two elements in the line otherwise there is just one element) a:1 a:1 123 b:2 345 c:3 456 d:4 d:4 456 .. .. I am interested in the output to be a:1 123 d:4 456 i.e lines which have the preceding field to have just one field. ...

replace white spaces with a comma in a txt file in linux

Hi all, I need to edit few txt files (an output from sar) and to convert them into csv files. therefore I need to change every white space (or maybe it's a tab between the numbers in the output) using sed or awk functions (an easy shell script in linux) can anyone help me? every command I used didn't change the file at all (I tried gsu...

What are some one-liners that can output unique elements of the nth column to another file?

I have a file like this: 1 2 3 4 5 6 7 6 8 9 6 3 4 4 4 What are some one-liners that can output unique elements of the nth column to another file? EDIT: Here's a list of solutions people gave. Thanks guys! cat in.txt | cut -d' ' -f 3 | sort -u cut -c 1 t.txt | sort -u awk '{ print $2 }' cols.txt | uniq perl -anE 'say $F[0] unless $...