Browsing articles tagged with "awk - Admins Goodies"
May 17, 2012
tom

Display nth instance of grepped pattern

So here’s my problem. I have an issue with a .csv file (Current.csv) in that there are commas randomly place throughout the data, so awk-ing the file doesn’t give me constant column numbers for a particular type of information I’m looking for. Luckily, I realized this info is always the third instance of a date format (m,mm)/(d,dd)/yy. So I’m trying the regular expression below to display just the dates within the ith line: awk -F’,’ […]

Continue Reading »
Apr 23, 2012
tom

How to pass the output of a command as a variable in a bash script?

I’m trying to pass the output of an awk command as a variable in a bash script, but not having much success so any help is appreciated. homedirectory=$(awk /stephen/ /etc/passwd | awk -F/home/ ‘{print $2}’ | awk -F/downloads ‘{print $1}’)echo ‘$homedirectory’ I want to extract the home directory of user stephen from the /etc/passwd, so the result I want is 0001 /etc/passwdstatd:x:102:65534::/var/lib/nfs:/bin/falsebind:x:103:106::/var/cache/bind:/bin/falsesshd:x:104:65534::/var/run/sshd:/usr/sbin/nologinmysql:x:105:107:MySQL Server,,,:/var/lib/mysql:/bin/falsemessagebus:x:106:108::/var/run/dbus:/bin/falsestephen:x:1039:1031::/home/0001/downloads:/bin/false Asked by Stephen Don’t look at /etc/passwd. echo ~username, in your case […]

Continue Reading »
Apr 19, 2012
tom

awk + perl + get two arguments in to awk syntax

please advice what wrong with my awk syntax and how to fix it ( this syntax is in my ksh script ) and I run my script on linux machine my target is to get from the file.txt only the lines that between the dates: FROM 2012-04-19 TO 2012-04-22 remark – other solution can be with perl [root@test1 /var/tmp]# a=’2012/04/19′ [root@test1 /var/tmp]# b=’2012/04/22′ [root@test1 /var/tmp]# awk -v A=$a -v B=$b ‘/A/,/B/’ file.txt awk: syntax error […]

Continue Reading »
Apr 2, 2012
tom

AWK and file names with space in it.

I’m trying to parse files with awk to change their names. Everything went well until im started to do this with files with space in file name. File names are something like 11237_712312955_2012-01-04 18_31_03.wav and I want to replace wav from file name. This is example of my code: ls | awk -F\. ‘{print $1}’ After i run this in console evething is ok and I get file name whithout extension. Example: file 11237_712312955_2012-01-04 18_31_03.wav […]

Continue Reading »
Mar 30, 2012
tom

bash/sed/awk/etc remove every other newline

a bash commands outputs this: Runtime Name: vmhba2:C0:T3:L14 Group State: active Runtime Name: vmhba3:C0:T0:L14 Group State: active unoptimized Runtime Name: vmhba2:C0:T1:L14 Group State: active unoptimized Runtime Name: vmhba3:C0:T3:L14 Group State: active Runtime Name: vmhba2:C0:T2:L14 Group State: active I’d like to pipe it to something to make it look like this: Runtime Name: vmhba2:C0:T1:L14 Group State: active Runtime Name: vmhba3:C0:T3:L14 Group State: active unoptimized Runtime Name: vmhba2:C0:T2:L14 Group State: active […] i.e. remove every other newline […]

Continue Reading »
Mar 8, 2012
tom

append to end of line sed / awk

This is probably easy and something is not clicking for me and my lack of coffee right now. I have a file with multiple lines that begin with a tab then the word GROUP something { Some of these lines for whatever reason drop the curly bracket under some conditions. The quick fix is to use sed/awk to append the curly bracket to that line but not lines where the bracket already exist. I’m halfway […]

Continue Reading »
Feb 29, 2012
tom

What command can I use to search sendmail logs for ALL message details involving a specific recipient.

CENTOS 5.x | Sendmail Occasionally I need to search through sendmail delivery logs to find out what happened to a missing message. This usually involves two (or more) steps: STEP 1: Search /var/log/maillog for the user’s email address. For example grep -i “someuser@recipientdomain.com” /var/log/maillog That usually returns something like this: Jan 11 07:43:34 server-example sendmail[12732]: p937blksdh3: to=<someuser@recipientdomain.com>, delay=00:00:00, xdelay=00:00:00, mailer=esmtp, pri=102537, relay=mta.recipientdomain.com. [12.34.56.78], dsn=5.7.1, stat=Service unavailable STEP 2: I’ll then grab the unique message name […]

Continue Reading »
Jan 31, 2012
tom

shell script + match MAJOR and CRITICAL strings from log file

I need to match lines that have MAJOR and CRITICAL strings that comes after the word ERROR:< any integer number > please advice how to do that with one awk or sed command? more HW_Log.txt CHK_HW ERROR:0 INFO self_monitor Verifying HW machine CHK_HW ERROR:1 MAJOR self_monitor Verifying HW machine CHK_HW ERROR:1 CRITICAL self_monitor Verifying HW machine Asked by Eytan This can be done using grep like: $ grep “ERROR:[0-9]* (CRITICAL|MAJOR)” /path/to/file Answered by Khaled Check […]

Continue Reading »
Jan 31, 2012
tom

Linux shell command to filter a text file by line length

I have a 30gb disk image of a borked partition (think dd if=/dev/sda1 of=diskimage) that I need to recover some text files from. Data carving tools like foremost only work on files with well defined headers, i.e. not plain text files, so I’ve fallen back on my good friend strings. strings diskimage > diskstrings.txt produced a 3gb text file containing a bunch of strings, mostly useless stuff, mixed in with the text that I actually […]

Continue Reading »
Jan 30, 2012
tom

Octal representation of file/folder permissions using awk?

We have various of machines here with all sorts of hardware and Operating Systems, most of them do regular tasks with bash that an agent executes. However it came to my attention that some Solaris machines we own do not have stat util, and adding it would be a problem that might take time (ironic) Meanwhile I was trying to imitate one sisyphean task which stat did in a magnificent way: return the file permissions […]

Continue Reading »
Pages:1234»