Multiple repetitions


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Multiple repetitions
# 1  
Old 01-03-2012
Multiple repetitions

In the below example, sed will consider the first field and prints it to twice..
Code:
% echo "123 abc" | sed 's/[0-9]*/& &/'
123 123 abc


Suppose if the input given is "123 abc def 345" then also it will consider only the 1st field.

If I require output as "123 abc def 345 345" i.e, sed should convert the given field where ever it is.
Code:
% echo "123 abc def 345" | sed 's/[0-9]*/& &/'

My expected output should be :
Code:
123 abc def 345 345

Can you please help me ?

Thanks in advance...


Moderator's Comments:
Mod Comment How to use code tags

Last edited by Franklin52; 01-03-2012 at 03:19 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 01-03-2012
Code:
% echo "123 abc def 345" | perl -e '$x=<>;$x=~s/(.* )([0-9]+)/$1$2 $2/; print $x'
123 abc def 345 345

# 3  
Old 01-03-2012
Thanks balajesuri

But could you please answer the same in sed?
# 4  
Old 01-03-2012
in awk ..
Code:
$ echo "123 abc def 345" | nawk '{$NF=$NF" "$NF; print}'
123 abc def 345 345

# 5  
Old 01-03-2012
Code:
echo "123 abc def 345" | sed 's/\(.* \)\([0-9]*\)/\1\2 \2/g'

---------- Post updated at 12:55 ---------- Previous update was at 12:49 ----------

@giridhar276: The above sed would repeat the last found number only if it is at the end of string. Use Perl's one-liner. It should work on all *nix systems.

@jayan_jay: I think giridhar276's requirement is to repeat the last found number in string. So if the string is "123 abc def 345 pqr", output should be "123 abc def 345 345 pqr".

Am I right giridhar276?
# 6  
Old 01-04-2012
try this.
Code:
echo "123 abc def 345" | sed 's/[0-9]\+/& &/2g'


Moderator's Comments:
Mod Comment How to use code tags

Last edited by Franklin52; 01-04-2012 at 03:38 AM.. Reason: Please use code tags for code and data samples, thank you
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to apply the update statement in multiple servers on multiple dbs at a time .?

Hi , Can any please help the below requirement on all multiple servers and multiple dbs. update configuration set value='yes' ;1) the above statement apply on 31 Databases at a time on different Ip address eg : 10.104.1.12 (unix ip address ) the above ip box contains 4 db's eg : db... (2 Replies)
Discussion started by: venkat918
2 Replies

2. Shell Programming and Scripting

sed parser behaving strange on replacing multiple words in multiple files

I have 4000 files like $cat clus_grp_seq10_g.phy 18 1002 anig_OJJ65951_1 ATGGTTTCGCAGCGTGATAGAGAATTGTTTAGGGATGATATTCGCTCGCGAGGAACGAAGCTCAATGCTGCCGAGCGCGAGAGTCTGCTAAGGCCATATCTGCCAGATCCGTCTGACCTTCCACGCAGGCCACTTCAGCGGCGCAAGAAGGTTCCTCG aver_OOF92921_1 ... (1 Reply)
Discussion started by: sammy777888
1 Replies

3. Shell Programming and Scripting

Ssh to multiple hosts and then run multiple for loops under remote session

Hello, I am trying to login to multiple servers and i have to run multiple loops to gather some details..Could you please help me out. I am specifically facing issues while running for loops. I have to run multiple for loops in else condition. but the below code is giving errors in for... (2 Replies)
Discussion started by: mohit_vardhani
2 Replies

4. Shell Programming and Scripting

Removing carriage returns from multiple lines in multiple files of different number of columns

Hello Gurus, I have a multiple pipe separated files which have records going over multiple Lines. End of line separator is \n and records going over multiple lines have <CR> as separator. below is example from one file. 1|ABC DEF|100|10 2|PQ RS T|200|20 3| UVWXYZ|300|30 4| GHIJKL|400|40... (7 Replies)
Discussion started by: dJHa
7 Replies

5. Shell Programming and Scripting

awk repeat one field at all lines and modify field repetitions

Hello experts I have a file with paragraphs begining with a keeping date and ending with "END": 20120301 num num John num num A keepnum1 num num kathrin num num A keepnum1 num num kathrin num num B keepnum2 num num Pete num num A keepnum1 num num Jacob num... (2 Replies)
Discussion started by: phaethon
2 Replies

6. Shell Programming and Scripting

Reading multiple values from multiple lines and columns and setting them to unique variables.

Hello, I would like to ask for help with csh script. An example of an input in .txt file is below, the number of lines varies from file to file and I have 2 or 3 columns with values. I would like to read all the values (probably one by one) and set them to independent unique variables that... (7 Replies)
Discussion started by: FMMOLA
7 Replies

7. Shell Programming and Scripting

Create Multiple UNIX Files for Multiple SQL Rows output

Dear All, I am trying to write a Unix Script which fires a sql query. The output of the sql query gives multiple rows. Each row should be saved in a separate Unix File. The number of rows of sql output can be variable. I am able save all the rows in one file but in separate files. Any... (14 Replies)
Discussion started by: Rahul_Bhasin
14 Replies

8. Shell Programming and Scripting

Grep from multiple patterns multiple file multiple output

Hi, I want to grep multiple patterns from multiple files and save to multiple outputs. As of now its outputting all to the same file when I use this command. Input : 108 files to check for 390 patterns to check for. output I need to 108 files with the searched patterns. Xargs -I {} grep... (3 Replies)
Discussion started by: Diya123
3 Replies

9. Shell Programming and Scripting

Awk match multiple columns in multiple lines in single file

Hi, Input 7488 7389 chr1.fa chr1.fa 3546 9887 chr5.fa chr9.fa 7387 7898 chrX.fa chr3.fa 7488 7389 chr21.fa chr3.fa 7488 7389 chr1.fa chr1.fa 3546 9887 chr9.fa chr5.fa 7898 7387 chrX.fa chr3.fa Desired Output 7488 7389 chr1.fa chr1.fa 2 3546 9887 chr5.fa chr9.fa 2... (2 Replies)
Discussion started by: jacobs.smith
2 Replies

10. Programming

Control multiple program instances - open multiple files problem

Hello. This shouldn't be an unusual problem, but I cannot find anything about it at google or at other search machine. So, I've made an application using C++ and QtCreator. I 've made a new mime type for application's project files. My system (ubuntu 10.10), when I right click a file and I... (3 Replies)
Discussion started by: hakermania
3 Replies
Login or Register to Ask a Question