How to use AWK regExp to print multiple substring pattern in a excel format in different column

Asked
Active3 hr before
Viewed126 times

7 Answers

differentmultiple
90%

Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers ,Thanks for contributing an answer to Stack Overflow!, 3 you'll have to show your own efforts to solve this.. and see stackoverflow.com/editing-help – Sundeep Jun 7 '17 at 5:23 ,Then you can simply use awk working on column $7 with some gensub( /regex/, substitution, n, column), awk's general substitution tool

If your desired Input looks like the file data:

$ cat file.log
10.0 .7 .92 - -[05 / Jun / 2017: 03: 50: 06 + 0000]
"GET /adserver/html5/inwapads/?category=[IAB]&size=320x280&ak=AY1234&output=vast&version=1.1&sleepAfter=&requester=John&adFormat=preappvideo HTTP/1.1"
200 131 "-"
"Mozilla/5.0 (Linux; Android 6.0.1; SM-S120VL Build/MMB29M; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/58.0.3029.83 Mobile Safari/537.36"
0.000 1029 520 127.0 .0 .1
10.0 .6 .91 - -[05 / Jun / 2017: 03: 50: 06 + 0000]
"GET /adserver/html5/inwapads/?category=[IAB]&output=vast&version=1.1&sleepAfter=&requester=John&size=320x280&ak=AY1234&adFormat=preappvideo HTTP/1.1"
200 131 "-"
"Mozilla/5.0 (Linux; Android 6.0.1; SM-S120VL Build/MMB29M; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/58.0.3029.83 Mobile Safari/537.36"
0.000 1029 520 127.0 .0 .1
load more v
88%

The above examples show only two different actions (print and exit) and a variety of tests based on matching substrings in each line, or on which line number (NR, which stands for “number of the record”).,Take a moment to make sure you understand which parts are the tests, and which are the actions in the above examples.,Note that if you don’t provide an action block after a test, the default action, if the test is true, is assumed to be “print the whole line as is.” Thus you can use awk to print matching lines simply like this:,When you want to test whether a line will be processed by an action block, or not, you have many options. Among the major ones (each given with an example or two) are:

git clone https: //github.com/eriqande/awk-and-sed-inputs
load more v
72%

Yet many newer Unix users are unaware of the classic tools and their power. , Tools built in the Unix tradition excel at manipulating strings as data. , Text editors in the Unix tradition excel at manipulating text. , The AWK philosophy melds well with the Unix tradition of storing data in ad hoc line-oriented databases, e.g., /etc/passwd.

Suppose you're playing the crosswords, and you know a word is seven letters long, with a for it second letter and x for the sixth. Get a hint:

 $ grep '^.a...x.$' / usr / share / dict / words
 cachexy
 carboxy
 martext
 panmixy
load more v
65%

This chapter describes the awk command, a tool with the ability to match lines of text in a file and a set of commands that you can use to manipulate the matched lines. In addition to matching text with the full set of extended regular expressions described in Chapter 1, awk treats each line, or record, as a set of elements, or fields, that can be manipulated individually or in combination. Thus, awk can perform more complex operations, such as: ,You can use magnitude-comparison operators to test strings. For example, the following pattern finds records that begin with "s" or any character that appears after it to the end of the character set: , An action is one or more steps to be executed, designated with awk commands, operands, and operators. Actions can consist of the following: ,In this structure, a pattern is one or more expressions that define the text to be matched. Patterns can consist of the following:

% awk '-F[Tab]'
'-f myprog'
report
load more v
75%

Notice that there’s no formatting of the output. There are many ways to format and structure the output in AWK. Checkout the printing section on the AWK user guide for more information on this.,Now we’ve selected a couple of columns to print out, let’s use AWK to search for a specific thing – a number we know exists in the dataset. Note that if you specify what fields to print out, AWK will print the whole line that matches the search by default.,We can also use AWK to select and print parts of the file. Let’s do this now.,So far, we’ve confirmed that we can use the if statement in AWK to return the rows that meet our conditional expressions when true. Check out the documentation on using control statements in AWK for more ways you can use conditionals to make decisions.

MarkerName Allele1 Allele2 Freq1 FreqSE P - value Chr Pos
rs2326918 a g 0.8510 0.0001 0.5255 6 130881784
rs2439906 c g 0.0316 0.0039 0.8997 10 6870306
rs10760160 a c 0.5289 0.0191 0.8107 9 123043147
rs977590 a g 0.9354 0.0023 0.8757 7 34415290
rs17278013 t g 0.7498 0.0067 0.3595 14 24783304
rs7852050 a g 0.8814 0.0006 0.7671 9 9151167
rs7323548 a g 0.0432 0.0032 0.4555 13 112320879
rs12364336 a g 0.8720 0.0015 0.4542 11 99515186
rs12562373 a g 0.7548 0.0020 0.6151 1 164634379
load more v
40%

One important thing to note and always remember is that the use of ($) in Awk is different from its use in shell scripting.,Field editing is very important when using Awk to filter text or strings, it helps you get particular data in columns in a list. And always remember that the use of ($) operator in Awk is different from that in shell scripting.,Under shell scripting ($) is used to access the value of variables while in Awk ($) it is used only when accessing the contents of a field but not for accessing the value of variables.,In this part of our Linux Awk command series, we shall have a look at one of the most important features of Awk, which is field editing.

Example 1: I have created a text file called tecmintinfo.txt.

# vi tecmintinfo.txt
# cat tecmintinfo.txt
load more v
22%

5 ways to reverse the order of file content,Swap every 2 lines in a file,5 important things to follow to be a fast learner,I want to split a very very large file on every Nth occurrence of a pattern into smaller files using awk.


$ cat file1
Item1, 200
Item2, 500
Item3, 900
Item2, 800
Item1, 600
load more v

Other "different-multiple" queries related to "How to use AWK regExp to print multiple substring pattern in a excel format in different column"