Visit Our UNIX and Linux User Community

Using sed inside system call in awk

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using sed inside system call in awk
# 1  
Old 07-19-2016
Using sed inside system call in awk

HI ,
I am trying to write a code where if a file system has / at the end then replace it with null but it should not affect root file system /

for example if
ip is /var/opt/home/ then o/p is /var/opt/home
but if ip is / then it should remain /.

for this i am using below code but no success

echo "hi/" |awk -F "/" -v line=$0 '{ if ($NF == "" && NF -eq 2 && $1 == "") print $0;else print system("echo " $line \"| (sed 's>/$>>')\");}'

I am getting error:
awk: { if ($NF == "" && NF -eq 2 && $1 == "") print $0;else print system("echo " $line \"| (sed s
awk: ^ backslash not last character on line
I have tried numerous combinations but no luck.

thanks for help

# 2  
Old 07-19-2016
That method is quite an overkill by the looks of it. You could simply do something like this (providing you have an bash/ksh that supports this form of parameter expansion):

echo ${tmp:-/} # The expected result

I imagine there are even smarter ways to.

Last edited by pilnet101; 07-19-2016 at 10:52 AM.. Reason: POSIX friendly now
# 3  
Old 07-19-2016
Hello Jcpratap,

It seems a very heavy way to achieve this. Could you do something based on the following instead?
echo $my_dir

By way of explanation, the variable is substituted for a value based on itself. It could be done in two steps for more clarity like this:-
my_dir="${my_dir%/}"        # 1st substitution
my_dir="${my_dir:=/}"       # 2nd substitution
echo $my_dir

The first substitution trims off a trailing / from whatever the input string is.
The second substitution assigns the value / if the input is null, i.e. it used to be just / before it was trimmed to nothing. The first code suggestion just merges these two together.

I hope that this helps,

Last edited by rbatte1; 07-19-2016 at 10:41 AM.. Reason: Put on greeting so I'm not answering pilnet191 posted moments before me
# 4  
Old 07-19-2016
Another approach:-
awk '/[^/]+/{sub(/\/$/,X)}1'

# 5  
Old 07-19-2016
You are spawning another process with that though and not very easy to understand if you are not a awk Jedi Master like yourself.
# 6  
Old 07-19-2016
@rbatte1: in my bash, my_dir="${my_dir%/:=/}" doesn't work. Is that a "bashism" or do I do sth. wrong?
# 7  
Old 07-20-2016

awk '/[^/]+/{sub(/\/$/,X)}1'


can you explain the same

Previous Thread | Next Thread
Test Your Knowledge in Computers #965
Difficulty: Medium
The HTML syntax requires a doctype to be specified to ensure that the browser renders the page in standards mode.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

sed inside the awk script to replace a string in the array

The requirement is i need to find an array value matching with pattern {5:{ , replace that with 5: and reassign that to same array index and print it. I write something like below and the issue is sed command is not working. If i replace " with "`" the script gives syntax can i... (8 Replies)
Discussion started by: bhagya123
8 Replies

2. Shell Programming and Scripting

awk statement piped inside sed

Hello folks, I have multiple occurrences of the pattern: ).: where is any digit, in various text context but the pattern is unique as this regex. And I need to turn this decimal fraction into an integer (corresponding percent value: the range of 0-100). What I'm doing is: cat... (1 Reply)
Discussion started by: roussine
1 Replies

3. UNIX for Dummies Questions & Answers

sed inside awk

What I want to do is delete everything upto the last underscore (_) in column 2. awk '{ $2=$(echo $2 | sed 's/.*_//'); print $0}' Sed works fine if I echo the string into it, this doesnt work inside awk. What am I doing wrong? Similarly, how do I store the substring starting with a... (4 Replies)
Discussion started by: senhia83
4 Replies

4. Shell Programming and Scripting

Invoking system(cmd) inside awk command

Hi, I was searching for a way to grep 2 lines before and after a certain keyword, and I came across the following code.. awk "\$0 ~ /ORA-/ { cmd=\"awk 'NR>=\" NR-2 \" && NR<=\" NR+2 \"' init.ora\" system(cmd) }" input_file I could not understand how this works. What is system() ? what... (2 Replies)
Discussion started by: Kulasekar
2 Replies

5. Shell Programming and Scripting

System call using awk on Solaris

I'm brand new to awk and need your help. I want to be able to shut down my workstations when they become to hot (because we've lost our cooling). I found a really neat way to monitor the temperature of the system and a short, simple awk scipt to use with it: /usr/sbin/prtpicl -v -c... (2 Replies)
Discussion started by: natural
2 Replies

6. Shell Programming and Scripting

Passing argument to system call in awk script

So, I have this script. It reads a CSV file that has a mixture of object names with IP addresses (parsing out that part I have working), and object names which have a DNS name. I want to be able to run a "dig +short" based off of the name given to me in the line of the awk script, and then deal... (6 Replies)
Discussion started by: mikesimone
6 Replies

7. Shell Programming and Scripting

Appending string (charachters inside the line) to a fixed width file using awk or sed

Source File: abcdefghijklmnop01qrstuvwxyz abcdefghijklmnop02qrstuvwxyz abcdefghijklmnop03qrstuvwxyz abcdefghijklmnop04qrstuvwxyz abcdefghijklmnop05qrstuvwxyz Whatever characters are in 17-18 on each line of the file, it should be concatenated to the same line at the character number... (6 Replies)
Discussion started by: tamahomekarasu
6 Replies

8. Shell Programming and Scripting

how to assign a value to several variables inside awk call

Hi I am using 'awk" to get file size and date of the file: op - sundev3 $ ls -l /bb/bin/tsfiles.sel | awk '{print $5 $6 $7}' 1587May8 May be somobody knows the way to combine this command with variable assignmet inside the awk, so I would have variables SIZE, MONTH and DATE assigned after... (1 Reply)
Discussion started by: aoussenko
1 Replies

9. UNIX for Advanced & Expert Users

how to differentiate system call from library call

Hi, Ho do I differentiate system call from library call? for example if I am using chmod , how do I find out if it is a system call or library call? Thanks Muru (2 Replies)
Discussion started by: muru
2 Replies

10. UNIX for Dummies Questions & Answers

a system call for sed in a awk script

Hi, this is my test file : DELETE FROM TABLE WHERE ID_INTERNAL = :TABLE.ID-INTERNAL, ID-INTERNAL-CRAZY ID-INTERNAL-OPEN ID-INTERNAL /ID-INTERNAL/ I want all occurences of ID-INTERNAL replaced with a one, if ID-INTERNAL has and dash afer it , dont replace it example:... (6 Replies)
Discussion started by: seaten
6 Replies

Featured Tech Videos