Print words starting at particular positions


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print words starting at particular positions
# 1  
Old 10-22-2013
Print words starting at particular positions

I have three words say abc def and ghi. I want to write them in a text file each starting (not ending) at particular positions say 1, 42 and 73. It is guaranteed that they will not overwrite.

Length of the three variable may vary.

Any help?
# 2  
Old 10-22-2013
This would be simple in something like perl or python or even C, but shell doesn't support this directly.

You can abuse the dd utility to do this, though it's not the most efficient form of access.

Code:
printf "%s" "abc "| dd of=myfile bs=1 conv=notrunc seek=42

Untested, so back up your file before you try this.
# 3  
Old 10-22-2013
Thanks. But this is not what I want. The output will be a single line consisting of values of abc, def and ghi starting at particular positions say 1, 42 and 73. (If I have to pring ending at those positions, printf command is handy). Use of awk is also fine.
# 4  
Old 10-22-2013
Quote:
Originally Posted by Soham
I have three words say abc def and ghi. I want to write them in a text file each starting (not ending) at particular positions say 1, 42 and 73. It is guaranteed that they will not overwrite.

Length of the three variable may vary.

Any help?
Code:
a="abc"
b="def"
c="ghi"
printf "%-41s%-31s%s\n" $a $b $c

Read up on printf to see what is happening, but basically I am telling it to left-justify the strings in the fields of the given lengths.

Andrew
This User Gave Thanks to apmcd47 For This Post:
# 5  
Old 10-22-2013
Try this:

Code:
$ cat infile
test one
12345678901234567890123456789012345678901234567890123456789012345678901234567890
two

$ awk '
 BEGIN{
    R[1]="abc"
    R[42]="def"
    R[73]="ghi"
    w=75}
 { if(length<w) $0=sprintf("%-"w"s", $0);
  for(r in R) $0 = substr($0,1,r-1) R[r] substr($0,r + length(R[r]))}1' infile
abct one                                 def                            ghi
abc45678901234567890123456789012345678901def5678901234567890123456789012ghi67890
abc                                      def                            ghi

---------- Post updated at 09:45 AM ---------- Previous update was at 08:12 AM ----------

Another approach is to define a function to do the work and call it as required, the output of this solution should be the same as the earlier one, it just might be a little easier to read/change/comment:

Code:
awk '
function overlay(s,f,v) {return substr(sprintf("%-*s",length(v)+f,s),1,f-1) v substr(s,f+length(v)) }
{
 $0 = overlay($0,  1, "abc")
 $0 = overlay($0, 42, "def")
 $0 = overlay($0, 73, "ghi")
}1' infile

This User Gave Thanks to Chubler_XL For This Post:
# 6  
Old 10-23-2013
Thanks apmcd47 and Chubler_XL. Works nice.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Search for words starting and ending with

im trying to search for a WORD in a file which begins with a number followed by a hypen follwed multiple words and end with a dot "." and pront the entire line which matches the above. Please note that there is a space at the begining of each line i/p file 19458 00000-CONTROL-PARA.... (5 Replies)
Discussion started by: anijan
5 Replies

2. Shell Programming and Scripting

Count the words starting with 3-

OS : Oracle Linux 6.5 Shell : bash I have a file whose contents look like below. I want to count the number of occurences of strings starting with 3-. How can I do this ? I couldn't wordwrap the below line. Hence it looks long. '3-90892405251', '3-90892911050', '3-90893144163',... (8 Replies)
Discussion started by: John K
8 Replies

3. Shell Programming and Scripting

Extract words starting with a pattern from a file

Hi Guys.. I have a file and i want to extract all words that starts with a pattern 'ABC_' or 'ADF_' For example, ABC.txt ---- INSERT INTO ABC_DLKFJAL_FJKLD SELECT DISTINCT S,B,C FROM ADF_DKF_KDFJ_IERU8 A, ABC_LKDJFREUE9_FJKDF B WHERE A.FI=B.EI; COMMIT; Output : ABS_DLKFJAL_FJKLD,... (5 Replies)
Discussion started by: Pramod_009
5 Replies

4. Shell Programming and Scripting

awk script replace positions if certain positions equal prescribed value

I am attempting to replace positions 44-46 with YYY if positions 48-50 = XXX. awk -F "" '{if (substr($0,48,3)=="XXX") $44="YYY"}1' OFS="" $filename > $tempfile But this is not working, 44-46 is still spaces in my tempfile instead of YYY. Any suggestions would be greatly appreciated. (9 Replies)
Discussion started by: halplessProblem
9 Replies

5. Shell Programming and Scripting

Using awk to print line starting with particular word

Hi Geeks, Consider this line: driver=c:\folder1\folder2 The above line is contained in a variable say 'var' . I want to copy everything after 'driver=' in to another variable say var2. Please tell me how can this be done. (8 Replies)
Discussion started by: ajincoep
8 Replies

6. Shell Programming and Scripting

How to remove all words starting from a matching word in a line

Hi Guys, I have a file like this: wwwe 1 ioie ewew yyy uuu 88 erehrlk 4 ihoiwhe lkjhassad lkhsad yyy mmm 45 jhash lhasdhs lkhsdkjsn ouiyrshroi oihoihswodnw oiyhewe yyy ggg 77 I want to remove everything after "yyy" and including "yyy" from each line in the file. So I want:... (2 Replies)
Discussion started by: npatwardhan
2 Replies

7. Shell Programming and Scripting

print only last two words of a line

can u help me out to print last two words of each sentence of a file. for example. contents of input file: i love songs my favourite songs sent songs all kind good buddy Ouput file should contain: love songs favourite songs sent all kind good buddy (5 Replies)
Discussion started by: pradeepreddy
5 Replies

8. Shell Programming and Scripting

Print starting 3rd line until end of the file.

Hi, I want to Print starting 3rd line until end of the file. Pls let me know the command. Thanks in advance. (1 Reply)
Discussion started by: smc3
1 Replies

9. UNIX for Dummies Questions & Answers

Starting & Stop print services in Unix

Hi There Kindly inform me on how to start and stop the print services in Unix from the command line. (2 Replies)
Discussion started by: esh
2 Replies
Login or Register to Ask a Question