LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Redirecting output of pipe operator to a variable (https://www.linuxquestions.org/questions/linux-software-2/redirecting-output-of-pipe-operator-to-a-variable-4175532885/)

zama 02-03-2015 01:55 AM

Redirecting output of pipe operator to a variable
 
I am using unix power tools to search for a content in a file and replace the content with sed command.

I am using the following command to achieve this

grep user ora | awk -F@ ' { print $1 } ' | awk -F= ' { print $2 } ' | cut -c 2- | xargs sed 's/user3/user/'

The contents of ora is as follows:

cat ora
user="user3@ghy" pass="ghy"

Every time the username will be different in 'ora' file. Different in the sense that user will remain same , the numeric part will change. e.g user1 , user2 , user3 etc .

Now I want that this username pattern will be replaced by fixed username namely 'user' .

grep user ora | awk -F@ ' { print $1 } ' | awk -F= ' { print $2 } ' | cut -c 2- | sed 's/<pattern>/user/'

I am using xargs command to achieve the result , but it is not working

grep user ora | awk -F@ ' { print $1 } ' | awk -F= ' { print $2 } ' | cut -c 2- | xargs sed -i 's//user/'
sed: can't read user3: No such file or directory

How to read this <pattern> dynamically.

chrism01 02-03-2015 05:19 AM

Code:

cat t.t
 user="user3@ghy" pass="ghy"
 user="user2@ghy" pass="ghy"
 user="user1@ghy" pass="ghy"

sed -i s/user[0-9]/user/ t.t

 cat t.t
 user="user@ghy" pass="ghy"
 user="user@ghy" pass="ghy"
 user="user@ghy" pass="ghy"

Does that help?

Alternate
Code:

for usr in user1 user2 user3; do sed -i s/$usr/user/ t.t; done


All times are GMT -5. The time now is 07:17 PM.