How do I replace the first 100 characters of all lines in a file using awk? There is no field delimiter in this file. All fields are fixed width. And given the variation in the data, I cannot use a search and replace.
Asked
Active
Viewed 130 times
1
-
Didn't work. the -r flag is illegal with my version. sed 's/.{46}/" "/' with855st – schwartzray Mar 04 '14 at 18:16
3 Answers
2
How about sed
? To replace the first 100 characters with say A
:
$ sed -r 's/.{100}/A/' file
If you're happy with the results rewrite the file using -i
:
$ sed -ri 's/.{100}/A/' file

Chris Seymour
- 83,387
- 30
- 160
- 202
-
Didn't work. the -r flag is illegal with my version. sed 's/.{46}/" "/' with855st – schwartzray Mar 04 '14 at 18:26
-
-
Okay this works. But I was hoping to replace the characters with an equal number of replacements--eg 100 blanks. I would just have to be literal with the replacement text I guess – schwartzray Mar 05 '14 at 23:22
-
In the end I did these set of commands. They worked for me. Thanks. * sed 's/.\{178\}//' with855st > with855st2 * nawk '{ sub(/^/, " ... "); print }' with855st2 > with855st3 * cut -c46-47 with855st > with855st3a * nawk 'FNR==NR{a[++i]=$0;next} {print a[FNR] "\t" $0}' with855st3a with855st3 > with855st4 * nawk '{ sub(/^/, "0000000000 "); print }' with855st4 > with855st5 – schwartzray Mar 06 '14 at 15:56
1
awk '{print "replacing text..." substr($0,100)}'

Sigi
- 4,826
- 1
- 19
- 23
-
Tried this and it didn't work. It basically deleted the whole line except for 2 blanks. nawk '{print " " substr($11,46)}' with855st – schwartzray Mar 04 '14 at 18:25
-
I think that you need to investigate more your file format then. This is basic gawk/nawk syntax, as you can find [here](http://heirloom.sourceforge.net/man/nawk.1.html). – Sigi Mar 04 '14 at 19:02
-
1
-
I used #11 because I ASSUMED, that was the starting column. I really wish to replace columns 11 thru 46 with blanks. – schwartzray Mar 05 '14 at 22:41
1
Use pure shell.
#!/usr/bin/env bash
# read each line into shell variable REPLY
while read -r ; do
echo "REPLACE text ... ${REPLY:100}"
done <file
Explanation
REPLY
is shell variable, refer http://www.gnu.org/software/bash/manual/html_node/Bash-Variables.html.Set to the line of input read by the read builtin command when no arguments are supplied
${REPLY:100}
- get the string after 100 characters.

BMW
- 42,880
- 12
- 99
- 116
-
+1: Did not know `REPLY` variable. Though you are **not** reading in an array. You are reading entire line in default variable. no? – jaypal singh Mar 05 '14 at 05:01
-
1
-
1