Help needed in character replacement in Korn Shell


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
# 1  
Question Help needed in character replacement in Korn Shell

How do I replace a space " " character at a particular position in a line?

e.g. I have a file below

Code:
$ cat i2
111 002 A a
33 0011 B c
2222 003 C a


I want all the 1st spaces to be replaced with forward slash "/" and the 3rd spaces to have 5 spaces to get the output below:


Code:
111/002 AAA     a
33/0011 BBB     c
2222/003 CCC     b

I created a shell which would achieve this but I'm looking for a easier way.

Code:
$ cat test.ksh
#!/bin/ksh

cut -d' ' -f1 < $1 > 1st_word
cut -d' ' -f3 < $1 > 3rd_word

while read line
do

sed -e "s/$line /$line\//g" $1 | grep $line

done < 1st_word > tmp_ouput


while read line
do

sed -e "s/$line /$line     /g" tmp_ouput1 | grep $line

done < 3rd_word


Any help will be appreciated

Steve
# 2  
Code:
$ cat file
111 002 A a
33 0011 B c
2222 003 C a
$ sed -e "s; ;/;1" -e "s/ /     /2" file
111/002 A     a
33/0011 B     c
2222/003 C     a

# 3  
Try this..

Code:
sed -e 's/ /\//' -e 's/\(.* .*\)\( .*\)/\1    \2/' filename

Output:


Code:
cat temp
111 002 A a
33 0011 B c
2222 003 C a
sed -e 's/ /\//' -e 's/\(.* .*\)\( .*\)/\1    \2/' temp
111/002 A     a
33/0011 B     c
2222/003 C     a

# 4  
Depending on your version of sed something like this may work for you:
Code:
$ sed -e 's! !/!1' -e 's/ /     /2' infile > outfile

If that doesn't work, the "longhand" version will....
Code:
$ sed -e 's/^\([^ ]*\) \([^ ]*\) \([^ ]*\) \(.*\)$/\1\/\2 \3     \4/' infile > outfile

Cheers
ZB
# 5  
Code:
sed 's/ /\//;s/ \(.\)$/    \1/' file

# 6  
Bug

That's exactly what I wanted!

Thank you all for your quick response!!!

Cheers
Steve
# 7  
Code:
sed 's/ /\//;s/ \(.\)$/    \1/' file

Hi matrixmadhan, can you explain what mean your code?

Thank you


regards,

heru
Login or Register for Dates, Times and to Reply

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

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help needed in Korn Shell scripting

#! /bin/ksh while read line do if ] ; then echo "no data" continue; fi echo "performing operation on $line" done < prg.txt (3 Replies)
Discussion started by: Juhi Kashyap
3 Replies

2. Shell Programming and Scripting

delete new line character ( - ) , korn shell

Hi guys , i need help so bad on this issue.. Basically i have to delete the line continuation symbol of first column variable and add the truncated part of that word in next line to first line. here i written sample 3 lines but originally i have bunch of lines in that file. client1_day- ... (3 Replies)
Discussion started by: chrismorgan
3 Replies

3. Shell Programming and Scripting

Line Replacement help needed.

I have a file called vm.cfg which looks like follows # cat vm.cfg acpi = 1 apic = 1 builder = 'hvm' device_model = '/usr/lib/xen/bin/qemu-dm' disk = kernel = '/usr/lib/xen/boot/hvmloader' memory = '300' name = 'vm_temp' on_crash = 'restart' on_reboot = 'restart' pae = 1 serial =... (5 Replies)
Discussion started by: pinga123
5 Replies

4. Programming

python, character replacement

Hello, I need to do this in python but I'm just learning py and it's quite urgent. I have a script I created and need an interactive python script to modify part of a line. MY SCRIPT #ligand prep. Uses 'ind' prefix. The initial ligand must be ind.pdb. This #generates ind.pdbqt.... (0 Replies)
Discussion started by: gav2251
0 Replies

5. Shell Programming and Scripting

Korn: How to loop through a string character by character

If I have a string defined as: MyString=abcde echo $MyString How can I loop through it character by character? I haven't been able to find a way to index the string so that I loop through it. shew01 (10 Replies)
Discussion started by: shew01
10 Replies

6. UNIX for Dummies Questions & Answers

banner character replacement

Can we able to replace the character # in banner command with some other characters. Can we able to blink the character in Kornshell (1 Reply)
Discussion started by: sivakumar.rj
1 Replies

7. Shell Programming and Scripting

Korn Shell script needed

Hi all, I need a Korn Shell script that will go out to all Unix Servers and pull all non humans IDs from them while at the same time produce a file to show which servers we have access to and which ones we don't. Thanks in advance. (5 Replies)
Discussion started by: vinsara
5 Replies

8. Shell Programming and Scripting

KSH - Character Replacement

Hey all. Easy question. I have a (ksh) varaible x. It contains the following (for example): N557788 I want to replace the "N" with a "-". I've done this before but for the life of me I cannot remember how I did it. Thanks. mtw (2 Replies)
Discussion started by: mixxamike
2 Replies

9. Shell Programming and Scripting

Character replacement

Hi, I am working on a command that replaces some occurrences of quotation marks in file. The quotation mark cannot be the first or the last character in line and cannot be preceded or followed by a comma. I am not an expert in regular expressions, but I managed to create the following... (2 Replies)
Discussion started by: piooooter
2 Replies

10. Shell Programming and Scripting

Korn Shell Help Needed

How do I change directories to a path given by input variable in Korn Shell? e.g. I tired with the Korn Shell below but it doesn't work. ---------------------------------- #!/bin/ksh echo "Enter folder name: \c" read folder cd $folder ---------------------------------- Any help will... (5 Replies)
Discussion started by: stevefox
5 Replies

Featured Tech Videos