LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 02-03-2015, 01:55 AM   #1
zama
Member
 
Registered: Mar 2012
Posts: 34

Rep: Reputation: Disabled
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.

Last edited by zama; 02-03-2015 at 03:06 AM.
 
Old 02-03-2015, 05:19 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 7.7 (?), Centos 8.1
Posts: 18,231

Rep: Reputation: 2708Reputation: 2708Reputation: 2708Reputation: 2708Reputation: 2708Reputation: 2708Reputation: 2708Reputation: 2708Reputation: 2708Reputation: 2708Reputation: 2708
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
 
  


Reply

Tags
awk, sed, xargs


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
range operator bash - change the output casperdaghost Linux - Newbie 1 10-31-2013 01:17 PM
the pipe operator AmphetaminePhreak Linux - Newbie 4 07-28-2006 08:39 PM
Redirecting sh output to a pipe elmafiacs Programming 0 05-10-2006 01:55 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 03:01 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration