Skip to main content
added 241 characters in body
Source Link
HardcoreHenry
  • 6.3k
  • 2
  • 23
  • 48

If you want to use sed, this would work:

~/tmp> str="directory_root /root/config/data/"
~/tmp> echo $str | sed 's|^[^/]*\(/[^/]*/\).*$|\1|'
/root/

Or a single liner (assuming directory_root literal is in the line:)

 cat file | sed -e 's|^directory_root \(/[^/]*/\).*$|\1|;tx;d;:x'

Explanation of regex in first example:

s| : using the | as the dilimiter (makes it easier to read in this case)

^ : match beginning of line

[^/]* : match all non / characters (this is greedy so it will stop when it hits the first /.

\( : start recording string 1

/ : match literal /

[^/]* : match all non / charcaters

\) : finish rcording string 1

.* : match everything else to the end of the line

| : delimitter

\1 : replace match with string 1

| : delimitter

In the second example, I appended the ;tx;d;:x which does not echo lines that do not match see here. You can then run this on the entire file, and it will only print the lines it modified.

~/tmp> echo "xx" > tmp.txt
~/tmp> echo "directory_root /root/config/data/" >> tmp.txt
~/tmp> echo "xxxx ttt" >> tmp.txt
~/tmp>
~/tmp> cat tmp.txt | sed -e 's|^directory_root \(/[^/]*/\).*$|\1|;tx;d;:x'
/root/

If you want to use sed, this would work:

~/tmp> str="directory_root /root/config/data/"
~/tmp> echo $str | sed 's|^[^/]*\(/[^/]*/\).*$|\1|'
/root/

Or a single liner (assuming directory_root literal is in the line:)

 cat file | sed -e 's|^directory_root \(/[^/]*/\).*$|\1|;tx;d;:x'

Explanation of regex in first example:

s| : using the | as the dilimiter (makes it easier to read in this case)

^ : match beginning of line

[^/]* : match all non / characters (this is greedy so it will stop when it hits the first /.

\( : start recording string 1

/ : match literal /

[^/]* : match all non / charcaters

\) : finish rcording string 1

.* : match everything else to the end of the line

| : delimitter

\1 : replace match with string 1

| : delimitter

In the second example, I appended the ;tx;d;:x which does not echo lines that do not match see here. You can then run this on the entire file, and it will only print the lines it modified.

If you want to use sed, this would work:

~/tmp> str="directory_root /root/config/data/"
~/tmp> echo $str | sed 's|^[^/]*\(/[^/]*/\).*$|\1|'
/root/

Or a single liner (assuming directory_root literal is in the line:)

 cat file | sed -e 's|^directory_root \(/[^/]*/\).*$|\1|;tx;d;:x'

Explanation of regex in first example:

s| : using the | as the dilimiter (makes it easier to read in this case)

^ : match beginning of line

[^/]* : match all non / characters (this is greedy so it will stop when it hits the first /.

\( : start recording string 1

/ : match literal /

[^/]* : match all non / charcaters

\) : finish rcording string 1

.* : match everything else to the end of the line

| : delimitter

\1 : replace match with string 1

| : delimitter

In the second example, I appended the ;tx;d;:x which does not echo lines that do not match see here. You can then run this on the entire file, and it will only print the lines it modified.

~/tmp> echo "xx" > tmp.txt
~/tmp> echo "directory_root /root/config/data/" >> tmp.txt
~/tmp> echo "xxxx ttt" >> tmp.txt
~/tmp>
~/tmp> cat tmp.txt | sed -e 's|^directory_root \(/[^/]*/\).*$|\1|;tx;d;:x'
/root/
added 173 characters in body
Source Link
HardcoreHenry
  • 6.3k
  • 2
  • 23
  • 48

If you want to use sed, this would work:

~/tmp> str="directory_root /root/config/data/"
~/tmp> echo $str | sed 's|^[^/]*\(/[^/]*/\).*$|\1|'
/root/

Or a single liner (assuming directory_root literal is in the line:)

 cat file | sed -e 's|^directory_root \(/[^/]*/\).*$|\1|;tx;d;:x'

Explanation of regex in first example:

s| : using the | as the dilimiter (makes it easier to read in this case)

^ : match beginning of line

[^/]* : match all non / characters (this is greedy so it will stop when it hits the first /.

\( : start recording string 1

/ : match literal /

[^/]* : match all non / charcaters

\) : finish rcording string 1

.* : match everything else to the end of the line

| : delimitter

\1 : replace match with string 1

| : delimitter

In the second example, I appended the ;tx;d;:x which does not echo lines that do not match see here. You can then run this on the entire file, and it will only print the lines it modified.

If you want to use sed, this would work:

~/tmp> str="directory_root /root/config/data/"
~/tmp> echo $str | sed 's|^[^/]*\(/[^/]*/\).*$|\1|'
/root/

Explanation:

s| : using the | as the dilimiter (makes it easier to read in this case)

^ : match beginning of line

[^/]* : match all non / characters (this is greedy so it will stop when it hits the first /.

\( : start recording string 1

/ : match literal /

[^/]* : match all non / charcaters

\) : finish rcording string 1

.* : match everything else to the end of the line

| : delimitter

\1 : replace match with string 1

| : delimitter

If you want to use sed, this would work:

~/tmp> str="directory_root /root/config/data/"
~/tmp> echo $str | sed 's|^[^/]*\(/[^/]*/\).*$|\1|'
/root/

Or a single liner (assuming directory_root literal is in the line:)

 cat file | sed -e 's|^directory_root \(/[^/]*/\).*$|\1|;tx;d;:x'

Explanation of regex in first example:

s| : using the | as the dilimiter (makes it easier to read in this case)

^ : match beginning of line

[^/]* : match all non / characters (this is greedy so it will stop when it hits the first /.

\( : start recording string 1

/ : match literal /

[^/]* : match all non / charcaters

\) : finish rcording string 1

.* : match everything else to the end of the line

| : delimitter

\1 : replace match with string 1

| : delimitter

In the second example, I appended the ;tx;d;:x which does not echo lines that do not match see here. You can then run this on the entire file, and it will only print the lines it modified.

Source Link
HardcoreHenry
  • 6.3k
  • 2
  • 23
  • 48

If you want to use sed, this would work:

~/tmp> str="directory_root /root/config/data/"
~/tmp> echo $str | sed 's|^[^/]*\(/[^/]*/\).*$|\1|'
/root/

Explanation:

s| : using the | as the dilimiter (makes it easier to read in this case)

^ : match beginning of line

[^/]* : match all non / characters (this is greedy so it will stop when it hits the first /.

\( : start recording string 1

/ : match literal /

[^/]* : match all non / charcaters

\) : finish rcording string 1

.* : match everything else to the end of the line

| : delimitter

\1 : replace match with string 1

| : delimitter