Text string modification


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Text string modification
# 8  
Old 11-10-2009
Shorter Smilie
Code:
echo 1234567890 | awk '{print $1$2$3$4"-"$5$6$7"-"$8$9$10}' FS=

# 9  
Old 11-10-2009
Quote:
Originally Posted by danmero
Shorter Smilie
Code:
echo 1234567890 | awk '{for(i=0;++i<=NF;)printf ((i~/4|7/)?"-"$i:$i)}' FS=

Although you write with shorter code, but the code is not more useful, if the input file has more than 1 line. Smilie

Code:
$ cat urfile 
1234567890
0987654321

$ awk '{for(i=0;++i<=NF;)printf ((i~/4|7/)?"-"$i:$i)}' FS= urfile
123-456-7890098-765-4321

And I don't understand why not use "for(i=1;i<=NF;i++)". Just for shorter code?

All other commands, which are run by awk, sed and echo, are still correct.

Code:
$ awk '{print substr($0,1,3),substr($0,4,3),substr($0,7,4)}' OFS="-" urfile
123-456-7890
098-765-4321

$ sed 's/\(.\{3\}\)/&-/1;s/\(.\{7\}\)/&-/' urfile
123-456-7890
098-765-4321

# 10  
Old 11-10-2009
Quote:
Originally Posted by rdcwayx
Although you write with shorter code, but the code is not more useful, if the input file has more than 1 line. Smilie
Ops, right Smilie
Code:
awk '{for(i=0;++i<=NF;)printf ((i~/4|7/)?"-"$i:((i==NF)?$i RS:$i))}' FS= file

# 11  
Old 11-10-2009
Code:
# echo 1234567890| gawk -vFS= '{$3=$3"-";$6=$6"-"}1' OFS=
123-456-7890

# 12  
Old 11-13-2009
thank you
is there a setting I can change so that I can get notified via e-mail after every new post and not just the first reply?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Search a string and display its location on the entire string and make a text file

I want to search a small string in a large string and find the locations of the string. For this I used grep "string" -ob <file name where the large string is stored>. Now this gives me the locations of that string. Now how do I store these locations in a text file. Please use CODE tags as... (7 Replies)
Discussion started by: ANKIT ROY
7 Replies

2. Shell Programming and Scripting

Help with text modification and displaying

I have a file storing some text and another file storing some numbers I want to display characters other than the specified place of strings one.txt xyz abc 233 skfo 4r443 sfs abc abcd sd fsdf sdfd abc 11 abc 33 abc dsaf two.txt Nt_djd_k='5-6,7-9' Nt_hh_l='3-6,7-8' a=`grep... (4 Replies)
Discussion started by: rahulsk
4 Replies

3. UNIX for Dummies Questions & Answers

Search String, Out matched text and input text for no match.

I need to search a string for some specific text which is no big deal using grep. My problem is when the search fails to find the text. I need to add text like "na" when my search does not match. I have tried this command but it does not work when I put the command in a loop in a bash script: ... (12 Replies)
Discussion started by: jojojmac5
12 Replies

4. Shell Programming and Scripting

Using sed to get text between a string and next line after another string

Hi folks! I'm trying to get a part of a text inside a text file (sudoers actually) but I'm having trouble. Here is an example of a text: Cmnd_Alias DUMMY = /bin/ls /bin/car /usr/bin/whatever Cmnd_Alias TARGET = test test test test test \ ... (6 Replies)
Discussion started by: leandrorius
6 Replies

5. Shell Programming and Scripting

Search a string in a text file and add another string at the end of line

Dear All I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB... (5 Replies)
Discussion started by: suryanarayana
5 Replies

6. Shell Programming and Scripting

Search a string in a text file and add another string at the particular position of a line

I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB and add/replace... (1 Reply)
Discussion started by: suryanarayana
1 Replies

7. HP-UX

Pid X killed due to text modification or page I/O error

Hello everybody, I have a HP-UX B.11.11. I had one disk and added one new. When trying to configure the second disk Not Using the Logical Volume Manager(from SAM) I have this error: Pid X killed due to text modification or page I/O error I tryed to add another partion on the first disk,... (5 Replies)
Discussion started by: savus
5 Replies

8. Shell Programming and Scripting

SED for text file modification

Hi all, i have a text file something like that FLAG DATE(YYYYMMDD) TIME(HHMMSS) FLAG1 DATE20060101 141216 FLAG1 DATE20070101 141216 FLAG2 DATE20060102 140010 FLAG2 DATE20060103 101212 FLAG1 ... (6 Replies)
Discussion started by: gfhgfnhhn
6 Replies

9. Shell Programming and Scripting

appending string to text file based on search string

Hi, I need to append string "Hi" to the beginning of the lines containing some specific string. How can I achieve that? Please help. Malay (1 Reply)
Discussion started by: malaymaru
1 Replies

10. Programming

Text Modification and page I/O error

Hi!, while launching my C application on HP-UX 10.2 workstation, I get the following error message sometime: Pid 9951 killed due to text modification or page I/O error Could someone tell me what does this error means?? and how is this caused? Thanx in Advance, JP (4 Replies)
Discussion started by: jyotipg
4 Replies
Login or Register to Ask a Question