1

Morning all,

I'm not very familiar with awk or filtering out values from a particular file.

The particular value i would like to extract from the file between "\n"

\nGenerated password: password\n

Is there a site i could potentially have a look at to help with this?

I managed to extract the information i need but having a bit of a difficult time to remove \n from the beginning and end of the line.

What i attempted: awk -F'\n' 'print $1'

Snippet of the output:

time="2023-01-23T18:40:06+02:00" level=info msg="Browser will open URL in 5 seconds"
time="2023-01-23T18:40:06+02:00" level=info msg="message"
time="2023-01-23T18:40:06+02:00" level=info msg="Open this link manually in a browser tab if it does not open."
time="2023-01-23T18:40:18+02:00" level=info msg="File encrypted \"file.txt\" in file.zip.\nGenerated password: mypassword\n"
konsolebox
  • 72,135
  • 12
  • 99
  • 105
Hein
  • 25
  • 4
  • Assuming there's no spaces in the password and `\n` is actually newline: `awk '/^Generated password: / { print $NF }'` – konsolebox Jan 24 '23 at 07:21
  • the exact line is printed in the file as "\nGenerated password: mypassword\n" I managed to get to the point using awk to only print out "\nGenerated password: mypassword\n" but would be nice to remove "\n" from the beginning and end of the output – Hein Jan 24 '23 at 07:32
  • 1
    Maybe show an actual and literal snippet of lines containing it. – konsolebox Jan 24 '23 at 07:35
  • time="2023-01-23T18:40:06+02:00" level=info msg="Browser will open URL in 5 seconds" time="2023-01-23T18:40:06+02:00" level=info msg="message" time="2023-01-23T18:40:06+02:00" level=info msg="Open this link manually in a browser tab if it does not open." time="2023-01-23T18:40:18+02:00" level=info msg="File encrypted \"file.txt\" in file.zip.\nGenerated password: mypassword\n" – Hein Jan 24 '23 at 07:43
  • Add this to your original post, click [edit](https://stackoverflow.com/posts/75218415/edit) – Gilles Quénot Jan 24 '23 at 07:44
  • A great introduction to `awk` and `sed` are the one-liners explained. You can find them [here (awk)](http://www.catonmat.net/blog/awk-one-liners-explained-part-one/) and [here (sed)](http://www.catonmat.net/blog/sed-one-liners-explained-part-one/). After that, you have enough basic knowledge to read the POSIX and GNU pages that explain you the full language. – kvantour Jan 24 '23 at 08:02
  • I'm putting my answer here b/c it might be useful to you: I'd use **GNU grep** if available, with perl lookbehind and lookahead patterns: `grep -Po '(?<=\\nGenerated password: ).*(?=\\n"$)'` You can adjust the patterns if necessary. For a literal backslash, escape with a second backslash. Note: Given that the output appears to be escaped/quoted (ie. `\n` and `\"`), you may need to handle the possibility of the password itself being escaped. – dan Jan 24 '23 at 10:36
  • I don't understand the `awk` program you posted, but: A field separator of a newline (without change of the record separator) would mean that each line is considered as a single field, and `print $1` would therefore simply print out the whole line. – user1934428 Jan 24 '23 at 10:39

2 Answers2

1
$ cat file
foo
Generated password: password
bar

$ awk '/^Generated password:/{printf "%s", $0}' file
-------8<------------------
Generated password: password

or to get only the value:

$ awk '/^Generated password:/{printf "%s", $NF}' file
-------8<------------------
password
Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
0

Using sed:

sed -n '/Generated password: / { s/\\n\".*//; s/.*\\n//; p }' file
konsolebox
  • 72,135
  • 12
  • 99
  • 105