Solved

how awk substr works and also how index works in inside substr?

Posted on 2016-10-20
4
158 Views
Last Modified: 2016-10-31
Hi Team,

i am little bit confused in in understanding the awk with substring function and also with index  which used in inside substring as below. Could anyone clearly explain me please? little bit confused :( :(

emerg1@HKLVADEDM10 ~/.ansible $ history | tail
 1952  2016-10-21 14:21:14 cat ../user_man/roles/usercreate/tasks/main.yml
 1953  2016-10-21 14:22:10 ls -lrt
 1954  2016-10-21 14:22:19 cat screenlog.1
 1955  2016-10-21 14:22:32 cd .ansible/
 1956  2016-10-21 14:22:33 ls -rlt
 1957  2016-10-21 14:22:39 cat user_add.yaml
 1958  2016-10-21 14:23:07 history | grep -i ansible | awk '{print sustr($0,index($0,$4))}'
 1959  2016-10-21 14:23:07 history | grep -i ansible | awk '{print sustr($0,index($0,$4))}'
 1960  2016-10-21 14:23:35 history | grep -i ansible | awk '{print substr($0,index($0,$4))}'
 1961  2016-10-21 14:25:11 history | tail


emerg1@HKLVADEDM10 ~/.ansible $  history | tail | awk '{print substr($0,index($0,$4))}'
ls -lrt
cat screenlog.1
cd .ansible/
ls -rlt
cat user_add.yaml
history | grep -i ansible | awk '{print sustr($0,index($0,$4))}'
history | grep -i ansible | awk '{print sustr($0,index($0,$4))}'
history | grep -i ansible | awk '{print substr($0,index($0,$4))}'
id gnanam
history | tail
emerg1@HKLVADEDM10 ~/.ansible $
0
Comment
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 14

Accepted Solution

by:
Phil Phillips earned 250 total points
ID: 41853358
substr returns a piece of a string. First argument is the string, and the second is the character position to start at. There is also a third optional argument of how many characters to return.

Example: substr("experts", 3) returns "perts"

Example: substr("experts", 3, 2) returns "pe"

Index returns the character position within a string. When you put substr and index together, you can get the piece of a string starting with the first occurrence of another string. In your example above, it's returning the 4th field and everything after.
0
 
LVL 79

Assisted Solution

by:arnold
arnold earned 125 total points
ID: 41854838
Awk by default splits the line based on white space. The individual entries start from 1
$0 means/represents the entire line
$4 is the forth element

As Phil covered, the result of your awk expression is to strip out all data prior to the fourth element while printing out the rest of the line from that point.
0
 
LVL 27

Assisted Solution

by:skullnobrains
skullnobrains earned 125 total points
ID: 41857077
yep. the whole command prints out all the commands that were run remotely using ansible ( assuming no options were given to ansible or you'd get some gibberish at the beginning of the lines ) whatever the remote host ( and possibly a few other commands containing the string 'ansible' )
0
 
LVL 1

Author Closing Comment

by:BharathKumarRaju DasaraRaju
ID: 41866493
Thank you for your help
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

632 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question