0

I am trying to get mount points and their respective paths on linux. So when I run the mount -v command I get this example output

//cifst/FSR on /mnt/share/cifst/FSR type cifs ...
//sydatsttbsq01/TheBooks statements to be parsed on /mnt/share/TheBooks type cifs ...

I am trying to parse this text to display this output

/mnt/share/cifst/FSR;//cifst/FSR
/mnt/share/TheBooks;//sydatsttbsq01/TheBooks

But the /mnt on the first row is in column 3, while on the second row is in column 5 so how do I do this to get the /mnt part

mount -v | grep mnt | awk '{ print $1'} gets me the path but how do I get the mount points.

Ishan
  • 3,931
  • 11
  • 37
  • 59

3 Answers3

0

For this particular output something like this will work; bear in mind that it will break if any of the paths with spaces that you use have the word "on" in them.

mount -v | awk 'BEGIN{FS="( on | type )"; OFS=";"} $3 ~ /cifs/ {print $2,$1}'
/mnt/share/cifst/FSR;//cifst/FSR
/mnt/share/TheBooks;//sydatsttbsq01/TheBooks statements to be parsed

P.S.: You'd be much better off if you didn't use spaces in paths, replace them with ., or _, or camelcase them ...

tink
  • 14,342
  • 4
  • 46
  • 50
  • Then your sample output above doesn't match what you have on your system. – tink Jul 22 '21 at 02:31
  • Yes I do. I have just been running it on my computer. Either you have a different version of awk or something else. Also for the paths its just `//sydatsttbsq01/TheBooks` the bit after that is not part of the path. – Ishan Jul 22 '21 at 02:32
  • It *is* part of the path, otherwise it wouldn't show up before the " on ". It's certainly not part of mount's output. As for awk - what version are you using? I'm using `gawk 5.0.1` – tink Jul 22 '21 at 02:35
  • I am using GNU Awk 5.0.1 – Ishan Jul 22 '21 at 02:42
  • Same awk then ... what Linux version are you running? – tink Jul 22 '21 at 02:43
  • I am using Windows SubSystem for Linux. – Ishan Jul 22 '21 at 02:45
  • WSL is notoriously bad w/ copy & pasting commands ... try TYPING what I wrote. – tink Jul 22 '21 at 02:46
  • I checked it copied exactly as you wrote. `awk 'BEGIN{FS="( on | type )"; OFS=";"}$3=="cifs"{print $2,$1}'` – Ishan Jul 22 '21 at 02:49
  • Think character substitution. It may *look* the same. – tink Jul 22 '21 at 02:51
  • OK, so here it's not encoding, it's a bug in `awk` in WSL. I tried this on a windows box, and it's not printing anything because the `$3=="cifs"` is failing in WSL. What **should** work just fine doesn't. If we replace the `==` with a regex match it works on windows, too. @Ishan - looks like WSL sucks. – tink Jul 22 '21 at 21:12
0

Lots of assumptions, but this works for your sample input/output:

$ cat << EOF | awk '{print $(NF-2), $1}' OFS=\;
> //cifst/FSR on /mnt/share/cifst/FSR type cifs
> //sydatsttbsq01/TheBooks statements to be parsed on /mnt/share/TheBooks type cifs
> EOF
/mnt/share/cifst/FSR;//cifst/FSR
/mnt/share/TheBooks;//sydatsttbsq01/TheBooks

The trick is to notice that it's not column 3 and 5 you're interested in, but in each case it is column NF - 2.

In this particular case, the grep is redundant because it matches each line of input, and in general grep is (almost) always redundant with awk. If you need to add the filter, do it with awk and use:

awk '/mnt/{print $(NF-2), $1}' OFS=\;
William Pursell
  • 204,365
  • 48
  • 270
  • 300
  • I updated the output in the question. There are a long line of text after `type cifs` on each line. I have used ... to indicate that. So what you suggested wont work. – Ishan Jul 22 '21 at 02:55
0

If the fields you are interested in are #1 and the next after the first field equal to "on", and they do not contain spaces, you could try this:

mount -v | awk '{a="";for(i=2;i<=NF;i++){if(a=="on")break;a=$i};print $i";"$1}'

If we add one more hypothesis that there is only one field equal to "on", another possibility is to use gensub:

mount -v | awk '{print gensub(/^(\S+).*\<on\>\s+(\S+).*/,"\\2;\\1",1)}'

Which brings us to a sed equivalent:

mount -v | sed -r 's/^(\S+).*\<on\>\s+(\S+).*/\2;\1/'
Renaud Pacalet
  • 25,260
  • 3
  • 34
  • 51