Help with replacing characters without moving the spaces.


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Help with replacing characters without moving the spaces.
# 1  
Old 08-23-2019
Help with replacing characters without moving the spaces.

Could you please advise what is the best way to edit a file without disrupting the spaces?

For example: I have within my file the value below, wherein I wanted to change VALUE2 to VALUETEST.
The total characters on the field of VALUE2 is 15 characters.

Code:
VALUE1|VALUE2<9 spaces>|VALUE3

but when I'm trying to use the command below. It keeps on adding new spaces that exceeds 15 characters.

Code:
sed s/VALUE2/VALUETEST/ <file>



Please help.

Last edited by vbe; 08-23-2019 at 01:19 PM..
# 2  
Old 08-23-2019
Maybe not doable with sed.
But with awk:
Code:
awk -v field=2 -v oldval="VALUE2" -v newval="VALUETEST"  -F"|"  '$field~oldval { $field=newval } { printf "%s|%-15s|%s\n", $1, $2, $3 }' file

# 3  
Old 08-23-2019
Something like s/VALUE2 /VALUE2AAAAA/ to not change the number of spaces.
# 4  
Old 08-23-2019
The following is even adaptive, using the * parameter in the printf format.
Code:
awk -v field=2 -v oldval="VALUE2" -v newval="VALUETEST" 'BEGIN { FS=OFS="|" } $field~oldval { len=length($field); $field=sprintf("%-*s", len, newval) } 1' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed replacing specific characters and control characters by escaping

sed -e "s// /g" old.txt > new.txt While I do know some control characters need to be escaped, can normal characters also be escaped and still work the same way? Basically I do not know all control characters that have a special meaning, for example, ?, ., % have a meaning and have to be escaped... (11 Replies)
Discussion started by: ijustneeda
11 Replies

2. Shell Programming and Scripting

Moving files that contain spaces...

I have a script that I've written and it's been running fine until someone dropped a file in the source directory that had spaces in it. The script breaks the file name into separate mv commands. I've tried putting " around the $FILE but that didn't help. Anyone who can help me would be greatly... (8 Replies)
Discussion started by: Sanglant
8 Replies

3. Shell Programming and Scripting

Replacing tabs with spaces

I want my program to replace tabs with spaces.1tab=4spaces.When i write aa(tab)aaa(tab)(tab)a(tab) it must show me aaxxaaaxxxxxaxxx. I think that my program works corectly but when a write aaa(tab)a it must show aaaxa but it is aaaxxxxxa.Please for help!!! That is my code: #include <stdio.h> ... (3 Replies)
Discussion started by: marto1914
3 Replies

4. Shell Programming and Scripting

Replacing the new character with spaces

Hi Experts, We are facing some while loading the "csv" file to target table.Some of the records are having values as : Account number,Name,Address "123","XYZ","302 Street,Washington,US" "456","PQR"," 3233 Some Street, Washington,US" In the above file instead reading only two records it... (11 Replies)
Discussion started by: Amey Joshi
11 Replies

5. Shell Programming and Scripting

trim spaces and replacing value

Hi, I have a file origFile.txt with values: origFile.txt .00~ 145416.02~ xyz~ ram kishor ~? ~ ~783.9 .35~ 765.76~ anh reid~ kelly woodburg ~nancy ~ ~? Now each row in the file has value for 7 columns with "~" as delimiter. The requirement was i)I need to erase the blank spaces between... (2 Replies)
Discussion started by: badrimohanty
2 Replies

6. Shell Programming and Scripting

moving files with spaces in filename from one directory to another

Hello, When I run following script #!/bin/bash cd ~/directory1 mv `ls -trF | grep -v / | tail -10 ` ~/directory2 works fine with filenames not having any space but runs into issues with filenames that have spaces tried with $file variable still doesnot work. Can someone help me (4 Replies)
Discussion started by: asakhare
4 Replies

7. Shell Programming and Scripting

Moving filenames containing spaces

I want to ftp all the sh files in the directory. Also if any of the file name contains spaces in them, it should be converted to underscores before it is ftped. I wrote the following code below: FILESSH=$(ls /mysh/*.sh) --- FILESH being used here for some other task --- echo "$FILESSH" |... (3 Replies)
Discussion started by: amicon007
3 Replies

8. UNIX for Dummies Questions & Answers

Replacing the Spaces

Hi, i have a tab dilimeted file.The records are :header is having column names.I am facing the following issue : I want to convert the spaces only for header file into "_" in the unix shell but the problem is that if i use sed command all the blank spaces are getting replaced by "_". For... (3 Replies)
Discussion started by: Amey Joshi
3 Replies

9. Shell Programming and Scripting

need help in replacing spaces in a file

hi all this is the part i am facing a problem eg data: filename : tr1 + T 40 this is a sample record in that file ... the value of T can be anything, but will be a single character. i need to cut from field two, and i am using this command cut -d " " -f2 tr1 >tr3 and the o/p is ... (7 Replies)
Discussion started by: sais
7 Replies

10. Shell Programming and Scripting

replacing tabs to spaces from files

hi, I have some 50 C files in which for indentation of code some devlopers used tabs, but we dont want any tab used for indentation. I have following 2 need. 1) find tabs from all 50 files (which are in one directory ) 2) replace them with 4 spaces. Thanks Rishi (6 Replies)
Discussion started by: rishir
6 Replies
Login or Register to Ask a Question