Home
Man
Search
Today's Posts
Register

BSD, Linux, and UNIX shell scripting Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Insert a user input string after matched string in file

Tags
file, input, shell scripts, string, user

Login to Reply

 
Thread Tools Search this Thread
# 8  
Old 09-02-2018
Unclear. I don't know what
Quote:
Originally Posted by tomislav91
. . .
I want something case1\nHELEN
. . .
should be.





If I enter 2 for uservar, this is what I get (after having adapted the case ... esac statement):
Code:
this is only a test
so please be patient
<color="red" />Helen

# 9  
Old 09-02-2018
Quote:
Originally Posted by RudiC
Unclear. I don't know what
should be.





If I enter 2 for uservar, this is what I get (after having adapted the case ... esac statement):
Code:
this is only a test
so please be patient
<color="red" />Helen

Idea is that i have multiple cases. Case 2 is case1 + some new string which i added.
Case 3 is case1+case2+some new string

------ Post updated at 05:06 AM ------

So if case one result is
123 text
Case2 will be
123 text new2
Case3 will be
123 text new2 new3
# 10  
Old 09-02-2018
That's NOT suitable for a case statement; use an array and a for loop:
Code:
ARR1=( 0 text1 text2 text3 text4 text5 )
uservar=4
for (( i=1; i<=uservar; i++)); do echo  ${ARR1[i]}; done
text1
text2
text3
text4


Last edited by RudiC; 09-02-2018 at 04:40 PM..
# 11  
Old 09-02-2018
I will try to combine with user input and variable match and text ehich i said earlier

------ Post updated at 08:58 AM ------

i did it like this
Code:
#!/bin/bash
read -p 'Enter number:' new
match='<color="red"  \/>'
text='text='this is only a test
so please be patient
<color="red" />''

text0='<h1> <!-- COmment -->
this is a test
    </h1>'
text1="MARIA"
text2="JACK"
text3="BRAD"
ARR1=( 0 $text0 $text1 $text2 $text3 )
uservar=$new
for (( i=1; i<=uservar; i++)); do echo "$text" | sed "s/$match/&\n ${ARR1[i]}/" ;done

So when execute it prints me only a
Quote:
Enter number:3
this is only a test
so please be patient
<color="red" />
this is only a test
so please be patient
<color="red" />
this is only a test
so please be patient
<color="red" />
It should print me
Quote:
Enter number:3
this is only a test
so please be patient
<color="red" />
MARIA
JACK
BRAD
------ Post updated at 08:58 AM ------

Quote:
Originally Posted by tomislav91
I will try to combine with user input and variable match and text ehich i said earlier

------ Post updated at 08:58 AM ------

i did it like this
Code:
#!/bin/bash
read -p 'Enter number:' new
match='<color="red"  \/>'
text='text='this is only a test
so please be patient
<color="red" />'

text0='<h1> <!-- COmment -->
this is a test
    </h1>'
text1="MARIA"
text2="JACK"
text3="BRAD"
ARR1=( 0 $text0 $text1 $text2 $text3 )
uservar=$new
for (( i=1; i<=uservar; i++)); do echo "$text" | sed "s/$match/&\n ${ARR1[i]}/" ;done

So when execute it prints me only a

It should print me
# 12  
Old 09-02-2018
So - what do you expect? It prints exactly what you told it to print. Did you do a test run with nothing but my proposal, unalterd?

Last edited by RudiC; 09-02-2018 at 04:12 PM..
# 13  
Old 09-02-2018
I dont know why dont print from var $text1, $text2 etc. Why only $text0

------ Post updated at 02:44 PM ------

I use for on my echo with sed..hmmm, i must to use for on array within sed, if I am correct...
# 14  
Old 09-19-2018
anyone?
Login to Reply

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Insert String every n lines, resetting line counter at desired string Skonectthedots Shell Programming and Scripting 1 03-17-2015 10:49 AM
Search String, Out matched text and input text for no match. jojojmac5 UNIX for Dummies Questions & Answers 12 01-07-2015 03:37 PM
Commenting a line matched with a specific string in a file raosr020 UNIX for Dummies Questions & Answers 3 03-27-2014 05:37 PM
String generation from user input Tobbev Shell Programming and Scripting 13 10-03-2012 10:01 AM
Matching string from input to string of file a_smith Shell Programming and Scripting 4 10-10-2011 03:40 PM
Grep a string from input file and delete next three lines including the line contains string in xml greet_sed Shell Programming and Scripting 4 09-28-2011 06:18 AM
input a string and copy lines from a file with that string on it engr.jay Shell Programming and Scripting 4 06-28-2011 07:36 AM
Extracting particular string in a file and storing matched string in output file sushmab82 Shell Programming and Scripting 14 03-11-2010 03:19 AM
save every line in log file with matched string meggae Shell Programming and Scripting 3 04-07-2009 12:38 AM
String extraction from user input - sh vino Shell Programming and Scripting 8 02-20-2005 10:54 AM


All times are GMT -4. The time now is 12:38 PM.

Unix & Linux Forums Content Copyright 1993-2018. All Rights Reserved.
UNIX.COM Login
Username:
Password:  
Show Password