Operating System - HP-UX
1819905 Members
2878 Online
109607 Solutions
New Discussion юеВ

Putting spaces into a cut command output

 
SOLVED
Go to solution
Wayne Clay
Occasional Advisor

Putting spaces into a cut command output

This may seem like a silly question, but I am cutting charaters from a flat file and there are no field seperators in the flat file. I want to grep for a piece of data from a line and cut data out of that line and put spaces between the data that is pulled out. Does anyone know of a way to do this?
9 REPLIES 9
John Meissner
Esteemed Contributor

Re: Putting spaces into a cut command output

using awk you can change any character into a field seperators....


awk -F. '{print $1,$2,$3}'

will put spaces between all characters in your file

(you'll need to put the appropriate number of fields in ...not just $1,$2,$3)
All paths lead to destiny
curt larson_1
Honored Contributor

Re: Putting spaces into a cut command output

seems like your just looking to replace a string with spaces, in which case sed should work just fine

cat your file | sed 's/ data / /g' #> another file

that is data with a leading and trailing space which means it won't catch if data is at the beginning or end of the line. and 6 spaces

sed 's/^data / /' #for beginning of line
sed 's/ data$/ /' #for end of line

these use 5 spaces.

altogether

sed -e 's/ data / /g' -e 's/^data / /' -e 's/ data$/ /'

just replace data with your characters and put in the appropriate number of spaces.
John Meissner
Esteemed Contributor

Re: Putting spaces into a cut command output

oops... sorry... no points for my previous post... didn't work ... I'll get the correct syntax to you in just a minute
All paths lead to destiny
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Putting spaces into a cut command output

The naive approach would do something like this:

cut -c 1-2,5-10 < myfile | while read A B
do
echo "${A} ${B}"
done

but this will not work because cut will put everying in the list argument in ${A}.

This will work:
cat myfile | while read X
do
A=$(echo ${X} | cut -c 1-2)
B=$(echo ${X} | cut -c 5-10)
echo "${A} ${B}"
done

A better approach would be to create an awk script, my.awk

{
a = substr($0,1,2)
b = substr($0,5,6)
printf("%s %s\n",a,b)
}

then invoke it as
awk -f my.awk myfile
If it ain't broke, I can fix that.
Wayne Clay
Occasional Advisor

Re: Putting spaces into a cut command output

What i'm trying to do is this:

I have a file with lines in it with data like this
0540000560583221010907500007477660505020711000000006
and I want to pull out charaters 13-23 and 44-52 with spaces between the first set of charaters and the last set. Also, the last set is the end of the line.
curt larson_1
Honored Contributor

Re: Putting spaces into a cut command output

which would mean a script something like this

#!/usr/bin/ksh

string="data"
numchar=${#string}
spaces=""

x=0
while (( $x < $numchar ))
do
spaces="$spaces "
done

cat yourfile |
sed -e 's/ '$string' / '$spaces' /g' -e 's/^'$string' / '$spaces' /' -e 's/ '$string'$/ '$spaces'/' > newfile

mv newfile yourfile
John Meissner
Esteemed Contributor

Re: Putting spaces into a cut command output

Sorry about my first post... I was thinking about something else... but this will work according to the information that you put in your last post


cat file |
while read line
do
sting1=$(echo $line | cut 13-23)
string2=$(echo $line | cut 44-52)
echo $string1 $string2 >> newfile
done

All paths lead to destiny
curt larson_1
Honored Contributor

Re: Putting spaces into a cut command output

if i think what you're looking right, clay's suggestion is probably best


cat yourfile |
awk '{
a = substr($0,13,11)
b = substr($0,44,9)
printf("%s %s\n",a,b)
}'

sorry about the misdirected posts
Wayne Clay
Occasional Advisor

Re: Putting spaces into a cut command output

Thank you all for your help. This has been bugging me all day.