awk - pattern-directed scanning and processing language
3
votes
4answers
1k views
AWK - how to reduce the greed of a regular expression?
I have an example:
echo "@article{gjn2010jucs, Author = {Grzegorz J. Nalepa}, " | awk '{ sub(/@.*,/,""); print }'
Is it possible to write a regular expression that selects the shorter pattern?
...
1
vote
3answers
45 views
Easy way to parse syslog date format
I'd like to write a script that analyses the last week's syslog.
But my syslog logs in a stupid date format, e.g. "May 22". If it logged as 2013-05-22, this task would be trivial.
Is there a way ...
5
votes
2answers
75 views
awk + paste for cleaning up PATH?
I have seen this code in .cshrc init files on a few machines. I went through a few awk tutorials in trying to understand how it works, but I am still unable to decrypt it.
setenv PATH `echo $PATH | ...
1
vote
1answer
36 views
Matching hexadecimals with gawk
I have a binary file with the following content (printed in hexadecimal):
$ xxd -p virus.com
5669727573b440bb0100b90500ba0000cd21
I can match the first 5 letters (which are the alphanumeric string ...
1
vote
2answers
121 views
Unix command for pattern matching
I want to count the number of words and print the matched pattern lines which matches exactly with following pattern:
abc-ERROR:
The input File contains:
# abc-ERROR: xyxxkkfgfr
# def-Error: ...
1
vote
2answers
50 views
awk a specific column with unexpected line break
I can't seem to find a solution for this and it's driving me crazy. I know I can use awk to print a column(s). I'm having trouble printing a specific column though because of the way my file system is ...
1
vote
1answer
208 views
Split log file by time range
I have a log file and would like to cut it into copies of 15min. In the log file the lines start with:
dd mmm yyyy hh:mm:ss,xxx
e.g.
12 Feb 2013 16:05:02,xxx log text...
and the file rotates ...
2
votes
3answers
37 views
Replace certain strings with value from former line
I have the following text:
Source IP,URL
1.1.1.1,example1.com
Blank,example2.com
2.2.2.2,example3.com
Blank,example4.com
Blank,example5.com
Blank,example6.com
Blank,example7.com
7.7.7.7,example8.com
...
1
vote
1answer
96 views
reading from a file and changing its pattern into an array?
I have a CSV file containging entries like this :
ipaddress,VLAN,VLANid
10.192.168.1,vlan-xyz,3
10.192.168.1,vlan-abc,8
10.192.168.1,vlan-mnp,11
10.192.163.24,vlan-llz,3
10.192.163.24,vlan-bbz,5
...
2
votes
2answers
236 views
xargs grep suggestion
grep -v "\<Swap" instruments.log | awk '{ idx=index($0, "MasterId="); masterId=substr($0, idx+length("MasterId=")+1); masterId=substr(masterId,1,index(masterId,"L")-3); print masterId; }' | xargs ...
3
votes
3answers
73 views
sed + how to remove character/s that start or ended on each number
How do I remove the . character(s) that start in the beginning of each number or end on each number?
Remark – perl one liner also good alternative for sed.
Example input:
.23.12.44.5.
.233.3.3.3
...
3
votes
3answers
66 views
checking data in columns when a data or some may be missing or present?
I am not sure if this possible.
say i have columns like :
Team Colour Game Rainfall PlayerName
XYZ Blue Cricket Yes Kapil
suppose i need to search ...
1
vote
2answers
34 views
Trimming one part if equal to another part?
I use an e-mail quote attribution string like this:
On dd mmm yyyy hh:mm +hhmm, from info@example.org (Example Dot Org):
where info@example.org is the sender's e-mail address and Example Dot Org is ...
5
votes
5answers
202 views
How to remove multiple blank lines from a file?
I have some text-files I use to take notes in - just plain text, usually just using cat >> file. Occasionally I use a blank line or two (just return - the new-line character) to specify a new ...
2
votes
2answers
33 views
confuse with awk user define function
I have solved Projecteuler Problem 3, but the thing is that I am not able to create the function in awk.
I have tried this working code (without function):
#!/usr/bin/awk -f
BEGIN{
...
1
vote
2answers
85 views
Problem using awk
I have a problem using awk. Print, from every file given as a parameter, the number of line that has the length at least 10. Also, print the content of that line(s), except the fist 10 characters. At ...
0
votes
1answer
71 views
Search low values in linux files
I am working with mainframe files and it seems that the low values (x'00') exists in the records in the file which throughs off my import for fixed block
Does anyone know how to eliminate this ...
1
vote
1answer
41 views
Using awk to parse AIX's lsuser output in stanza format
I'm creating a simple awk script to parse AIX's lsuser -f ALL output and return information in one line. A sample of the output is something like this.
root:
id=0
pgrp=something
...
0
votes
2answers
140 views
Select/extract columns based on the values on a certain row
Here is an example text file:
A B C D E F G
1 2 3 4 5 6 7
2 3 4 5 6 7 8
3 4 5 6 7 8 9
I would like to extract specific columns based on the values on the 3rd row, that is, 2 3 4 5 6 7 8.
Let's say, ...
2
votes
6answers
87 views
How can I separate these two outputs using awk command
Here's my output:
MessageID=3990592283244651750-30192b51.13df831d93a.7eb4;EsbTiming=2013-04-12T01:07:46.099
I want only the time.
How can I separate them?
0
votes
1answer
22 views
moving columns across rows
I am dealing with a txt file and trying to keep the first column/field in place, but shift the 2nd column down by 1 row. I tried this, but it gave the error unmatched .'s.
awk '{for(i=1; i<NR; ...
10
votes
6answers
2k views
How to print the longest line in a file?
I'm looking for the simplest method to print the longest line in a file. I did some googling and surprisingly couldn't seem to find an answer. I frequently print the length of the longest line in a ...
2
votes
3answers
119 views
How can I use SED or AWK to replace placeholders in a template file with variable content that contains special characters?
CentOS 6.3
I'm trying to get a small script to send an email containing a copy of email headers in the body (for the purpose of internal reporting).
The template file contains the following:
...
5
votes
3answers
2k views
How to sed only that lines that contains given string?
INPUT:
Select ASDF 325 sdfg sdflk lk
Select TRG 46sdg rasdftz fsgs 45
Select ASDF 6ffg sdfg 4456 sdrg
OUTPUT:
Select ASDF 325 XXXX sdflk lk
Select TRG 46sdg rasdftz fsgs 45
Select ASDF 6ffg XXXX ...
3
votes
2answers
47 views
Automated partition selection in bash script
I'm trying to write a bash script to run from Linux on a CD or flashdrive that can mount Windows and change stuff, but I don't know how to go about detecting which partition/drive is Windows.
My best ...
4
votes
3answers
96 views
Remove string from a particular field using awk/sed
I have a file (>80,000 lines) that looks likes this:
chr1 GTF2GFF chromosome 1 249213345 . . . ID=chr1;Name=chr1
chr1 GTF2GFF gene 11874 14408 . + . ...
4
votes
4answers
102 views
How to remove character and space from a string
Hello I have one file with output
Name : ABC
Name : CDE
Name : ZYS
I want to get the result
ABC
CDE
ZYS
How can i get it through SED command.
I have tried. sed s/'Name' but ...
15
votes
9answers
947 views
Remove duplicate $PATH entries with awk command
I am trying to write a bash shell function that will allow me to remove duplicate copies of directories from my path environment.
I was told that it is possible to achieve this with a one line ...
0
votes
1answer
94 views
comparing lines with awk vs while read line
I have two files one with 17k lines and another one with 4k lines. I wanted to compare position 115 to position 125 with each line in the second file and if there is a match, write the entire line ...
1
vote
1answer
56 views
Regex to Cisco Interface
I would like to know if have some Sed/Grep or Awk regex to parse Cisco interface section and exclude if interface is down (shutdown), like example bellow.
Example:
!
interface FastEthernet0/1
ip ...
4
votes
3answers
3k views
Delete range of lines above pattern with sed (or awk)
I have the following code that will remove lines with the pattern banana and 2 lines after it:
sed '/banana/I,+2 d' file
So far, so good! But I need it to remove 2 lines before banana, but I can't ...
7
votes
6answers
1k views
Is there any alternative to grep's -A -B -C switches (to print few lines before and after )?
grep -A 2 -B 3
prints 2 lines after the grep string and prints 3 lines before.
grep -C 3
prints 3 Lines before and 3 lines after
Unfortunately, the grep I'm using does not support these ...
5
votes
4answers
135 views
Show sum of file sizes in directory listing
The Windows dir directory listing command has a line at the end showing the total amount of space taken up by the files listed. For example, dir *.exe shows all the .exe files in the current ...
2
votes
2answers
63 views
Using bash shell function inside AWK
Is it possible to use bash function inside AWK somehow?
Example file (string, int, int, int)
Mike 247808 247809 247810
Trying to convert values from decimal to hexadecimal.
Function defined ...
3
votes
1answer
116 views
Pass colors from ls through pipe to awk 'print' statement
This is a follow-up to my question from yesterday, Show sum of file sizes in directory listing.
Thanks to Zero Piraeus and a point in the right direction by Mauritz Hansen, I now have
function ...
1
vote
3answers
63 views
awk + print line only if the first field start with string as Linux1
how to print the line in case the first field start with Linux1
for example:
echo Linux1_ver2 12542 kernel-update | awk '{if ($1 ~ Linux1 ) print $0;}'
the target is to print the line , ...
3
votes
1answer
91 views
Output of `java -version` not matched by grep or awk
On my Linux machine, it isn't clear to me why if I do the following then I don't get only the version string ("1.5.0_32").
# java -version | grep version | awk '{print $NF}'
java version "1.5.0_32"
...
2
votes
2answers
51 views
cumulative totals with shifted column
Apologies in advance since I am sure this is an easy problem. Nevertheless, here goes.
cat blah.txt
aa+2
bb+4
cc+10
dd+31
I would like to process blah.txt to produce the folowing:
aa+0
bb+2
cc+6
...
1
vote
1answer
41 views
Gawk: Passing arrays to functions
Stuck with GNU awk 3.1.6 and think I've worked around its array bugs but still have what looks like a scope problem in a 600-line awk program. Need to verify understanding of array scope in awk to ...
1
vote
1answer
51 views
awk - gensub function
I need to insert contents of variable to parameter function gensub in awk, specifically instead of regular expression's parameter. Can you help me with it?
I have:
gawk '
BEGIN {
a = ...
1
vote
1answer
67 views
Safely convert unicode strings to printable characters
I have many HTML files containing mixed unicode strings like \303\243 and printable characters like %s. What I'd like to do is converting the unicode strings into printable characters in a safe way.
...
0
votes
2answers
86 views
Reordering strings in linux
How can I change lines around using the command line?
e.g. I have these lines:
Acct-Status-Type = Start
User-Name = "37XXXXXXX"
Event-Timestamp = "Apr 12 2013 15:56:55 AMT"
I need to ...
0
votes
1answer
49 views
Regex awk to Cisco Interface
I would like to know if have some Sed/Grep or Awk regex to parse Cisco interface section, with specific attribute, like bellow.
Content of file.txt
!
interface FastEthernet0/1
no ip unreachables
!
...
1
vote
3answers
210 views
Newbie struggles with grep, sed, awk to filter html
I am a beginner linux user, trying to teach myself how to use linux tools on a cygwin install. I decided to make up a project to try to teach myself the basics of shell scripting and simultaneously ...
2
votes
1answer
72 views
Vim - how to increase each number in visual block?
I have the following SQL:
update am.PERMISSIONS set PRM_ORDER = 35 PRM_VISIBLE = b'1' where PRM_ID = 3;
update am.PERMISSIONS set PRM_ORDER = [35] PRM_VISIBLE = b'1' where PRM_ID = 7;
update ...
0
votes
1answer
55 views
replacing text and deleting text using awk in file using
I have a script and I want to do some replace and delete operations on it. I used sed to extract values between words SendCommands and end like this
sed -n '/SendCommands/,/end/p' ddtt
As awk was ...
1
vote
2answers
46 views
How do I get a field in bash's job list using awk?
I wanted to write a function to retrieve a field from the list of background jobs in bash. For example to get a specific argument.
Let's say I send vim to the background, so "jobs" displays this ...
-1
votes
3answers
63 views
Sorting IP address according to the second field in file
in my ksh script I need to add the following task ( OS - linux/solaris )
I have the following file
more test.txt
/etc/backup/app 172.1.120 172.110.120
/etc/backup/app 172.1.120.12 ...
-4
votes
1answer
47 views
How to transform two delimited ASCII files
These are 2 common delimited, text file transformation problems I'd like to find scripts to do (I dont know if awk or perl is best for these tasks).
csh is the default shell on my Solaris10 unix ...
1
vote
1answer
46 views
Replacing string based on line number
I have a situation where i want to replace a particular string in many files
Replace a string AAA with another string BBB but there are lot of strings starting with AAA or ending in AAA ,and i want ...





