Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have find output as follows:

/var/data/run/stores/user.rstd/info.settings:4: Password = "xxxxxxx";

In this example, "user" and the value of "Password" ("xxxxxxx";) need to be redirected to a new file. the "user" will always be in the 5th directory in. However I do not want the .rstd extention. My new file output should look like this

user   "xxxxxxxx";

How can I best accomplish this?

share|improve this question
    
What have you tried so far? – John1024 yesterday
    
I'm trying to do this with grep but not having any luck so far. I can get the password field but can't figure out how to get the "user" – user53029 yesterday
    
took a stab at it with sed to get just the user - sed -i -e 's/\/var\/data\/run\/stores\///g' but did not work. – user53029 yesterday
1  
@user53029 , well that is one way to approach.. you could add another substitution in the same command to get your output.. like sed -e 's/\/var\/data\/run\/stores\///' -e 's/\..*=//' .. once you are okay with output, you can add the -i flag for inplace editing.. there are many different ways the regex could be written – sp asic yesterday
    
Is this literally just one line? Or is the find putting out many lines like this. If so, does the output go to a single file full of users, or does each <user> "<password>" pair go into its own file? – Kaz 11 hours ago

Using sed

For convenience, let's put our find output into shell variable s:

$ s='/var/data/run/stores/user.rstd/info.settings:4: Password = "xxxxxxx";'

Now, let's extract the parts that you want:

$ echo "$s" | sed -E 's|/([^/]*/){4}([^/.]*)[.][^"]*(.*)|\2 \3|'
user "xxxxxxx";

How it works

The sed script consists of a single substitute command. The -E flag is used to enable extended regular expressions.

  • The line is matched against: /([^/]*/){4}([^/.]*)[.][^"]*(.*)

    /([^/]*/){4} matches the first four directories. Because of the parens, the last of these directories is saved as group 1 but we will have no use for it.

    ([^/.]*) matches the user name without the extension. Because of the parens, this grouping is saved as group 2.

    [.][^"]* matches the extension and everything up to the first double-quote.

    (.*) matches everything starting with the first double-quote to the end of the line. Again, because of the parens, this is saved as group 3.

  • The replacement text is \2 \3 which means group 2 followed by a space followed by group 3.

Using awk

$ echo "$s" | awk -v FS="/" '{ name=$6; sub(/[.].*/,"", name); sub(/[^"]*/, ""); print name, $0;}'
user "xxxxxxx";

How it works

  • -v FS="/"

    This sets the field separator to /.

  • name=$6; sub(/[.].*/,"", name)

    As awk counts fields, the name is in the sixth field. We save the sixth field in the variable name and then remove everything in name after the first period.

  • sub(/[^"]*/, "")

    This removes everything from the line up to but not including the first ".

  • print name, $0

    This prints the name, a field separator (default is a space), and what's is left of the line after the substitution (the password).

share|improve this answer

You can do that using sed grouping,

$ echo '/var/data/run/stores/user.rstd/info.settings:4: Password = "xxxxxxx";' | \
sed 's,\(^/var.*stores/\)\(user\)\(.rstd.*= \)\(.*\),\2  \4,'
user  "xxxxxxx";

I know it looks weird ;) alternatively you can use much simpler one

sed -e 's,/var/data/run/stores/,,;s,\..*=,,'
share|improve this answer
1  
You could use the r flag (extended regex mode) to avoid having to escape parentheses. – someonewithpc yesterday

A long one... but seems working

$ echo '/var/data/run/stores/user.rstd/info.settings:4: Password = "xxxxxxx";'  | \
awk -F/ ' { print $6 $7} ' | awk -F"." ' { print $1, " ", $3 } ' | \
awk ' { print $1 " " $NF } '
user "xxxxxxx";
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.