Visit The New, Modern Unix Linux Community


Issue with user input including * (glob) and sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Issue with user input including * (glob) and sed
# 1  
Issue with user input including * (glob) and sed

Hello All,

I have created a script that searches for different things and "sanitizes" the findings from files. Currently the user is required to put in a hostname (server.serverfarm.abc) one at a time to replace. I would like the user be able to use *.*.abc in grep and then pipe into sed to make changes.

I am currently stuck on having sed actually replace grep findings that include *.*.abc

I have tried different shopt options with no success. I believe it might be how i am writing the string (escaping characters etc). If someone could take a look at this that would be great!

Code:
#Function for Hostnames
hostcheck(){
       grep -Eilrs --exclude='.*' --exclude='*.sh' *.*.abc [directory] | 
       xargs -d "\n\n" sed -i "s/$hostname/$change/Ig"
                  }

I can provide more of the script if needed. Grep finds but when piped into sed it actually doesn;t do anything.

The variables are put in by the user. They are asked a hostname and input. They are asked what will replace ($change) the $hostname. Directory is also input from the user.

Any suggestions with using sed before i move on to a different tool? thanks!

Moderator's Comments:
Mod Comment edit by bakunin: Please use CODE-tags for code, data and terminal output as required by the rules. Thank you.


--- Post updated at 10:46 PM ---

Hello All,

The following seems to work:

Code:
grep -Eilrs --exclude='.*' --exclude='*.sh' *.*.abc | xargs -d "\n\n" sed -r "s/.*.*.abc/xx.xx.xx/Ig"

I am plugging this into the much larger script and will test from there. Once again, with many ways to skin a cat, I would be interested how i could improve upon this, thanks!

--- Post updated at 10:47 PM ---

Correction:
Code:
grep -Eilrs --exclude='.*' --exclude='*.sh' *.*.abc | xargs -d "\n\n" sed -ir "s/.*.*.abc/xx.xx.xx/Ig"

Moderator's Comments:
Mod Comment edit by bakunin: Please use CODE-tags for code, data and terminal output as required by the rules. Thank you.

Last edited by bakunin; 01-07-2019 at 06:45 PM..
# 2  
Some thoughts:



*.*.abc as used for grep seems to be a "glob" but should be a regex same as the one used in the sed script (where, BTW, the . is a wildcard char and should be escaped for the extension separator).
Simply using sed on ALL files (leaving them intact if no pattern found) might be more efficient than grepping through all of them, and then using multiple sed invocations again on those with the pattern - depends on the average file length, and the ratio of files with vs. without pattern.
With awk, you could use one single invocation working on an input stream of all files writing to respective individual temporary output files.

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #141
Difficulty: Easy
The original Unix operating system was not written in assembly language.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Including Hash / in sed command filter

Hello All, I want to print data in between two lines in a file sample.txt through more or cat command on the screen. For that I am using below sed command to give the BEGIN and END text. Content of sample.txt server01:~ # cat /proc/mdstat Hello this is a text message 1 Hello this is a... (5 Replies)
Discussion started by: Xtreme
5 Replies

2. Shell Programming and Scripting

Remove bracket including text inside with sed

Hello, I could not remove brackets with text contents myfile: Please remove the bracket with text I wish to remove: I tried: sed 's/\//' myfile It gives: Please remove the bracket with text A1 I expect: Please remove the bracket with text Many thanks Boris (2 Replies)
Discussion started by: baris35
2 Replies

3. Shell Programming and Scripting

Deleting "user input line number" from a file using sed

Hi I want to delete a line from a txt file for which the line number is user input. Say when user selects 19, the 19th line would be deleted from the file. Can anyone please provide me with a sed one liner for the same... I tried sed -i. The interaction would be like this Enter the line... (4 Replies)
Discussion started by: sudeep.id
4 Replies

4. Shell Programming and Scripting

How to get the user input recursively until the user provides valid input

Hi, echo "Enter file name of input file list along with absolute path : " read inputFileList if then for string in `cat inputFileList` do echo $string done else echo " file does not exist" fi From the above code, if the user enters a invalid file... (1 Reply)
Discussion started by: i.srini89
1 Replies

5. UNIX for Dummies Questions & Answers

Deleting "user input line number" from a file using sed

Hi I want to delete a line from a txt file for which the line number is user input. Say when user selects 19, the 19th line would be deleted from the file. Can anyone please provide me with a sed one liner for the same... I tried sed -i. The interaction would be like this Enter the line to... (1 Reply)
Discussion started by: sudeep.id
1 Replies

6. Shell Programming and Scripting

Grep a string from input file and delete next three lines including the line contains string in xml

Hi, 1_strings file contains $ cat 1_strings /home/$USER/Src /home/Valid /home/Review$ cat myxml <projected value="some string" path="/home/$USER/Src"> <input 1/> <estimate value/> <somestring/> </projected> <few more lines > <projected value="some string" path="/home/$USER/check">... (4 Replies)
Discussion started by: greet_sed
4 Replies

7. Shell Programming and Scripting

sed: remove characters between and including 2 strings

I have the following line: 4/23/2010 0:00:38.000: Copying $$3MSYDDC02$I would like to use sed (or similiar) to remove everthing between and including $ that appears in the line so it ends up like this. 4/23/2010 0:00:38.000: Copying 3MSYDDC02I have been trying these but i'm really just... (5 Replies)
Discussion started by: jelloir
5 Replies

8. Shell Programming and Scripting

How a normal user run a script including root privileaged commands

Dear all Ihave written a script in Hpux9.0, the ecript is working fine if I run it from root command prompt But when I am running it thru /etc/profile or /user/.profile and login as a normal user, the owner of the process running the script is the normal user & hence cant run a root privileaged... (7 Replies)
Discussion started by: initin
7 Replies

9. Shell Programming and Scripting

How to use sed to remove html tags including text between them

How to use sed to remove html tags including text between them? Example: User <b> rolvak </b> is stupid. It does not using <b>OOP</b>! and should output: User is stupid. It does not using ! Thank you.. (2 Replies)
Discussion started by: alphagon
2 Replies

10. Shell Programming and Scripting

Accepting user input in Bourne shell and using sed

He guys. Basically I want to make a script that can add, delete and view stuff in a external file called config.txt. I can open it up in Joe but im not sure how to read in the user input or using commands automatically in joe to edit, save then quit. Problem area below: 1) echo "Add... (1 Reply)
Discussion started by: Pits
1 Replies

Featured Tech Videos