Cut command skips spaces


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Cut command skips spaces
# 1  
Old 08-16-2010
Cut command skips spaces

Hi everyone

I have a file looks like this

Code:
 
X01 1   JOE      20100312
X02     TOD      20100312
X03   3 SAM      20100312

I wrote a script to assign the values in 5 columns to 5 variables. When I use cut command to assign column 2 or 3, it skips it if it is a space (see below)
Code:
while read LINE
   do
     VAR1=`echo $LINE | awk '{ print $1 }'`
     VAR2=`echo $LINE | cut -c 5`
     VAR3=`echo $LINE | cut -c 7`
     VAR4=`echo $LINE | awk '{ print $4 }'`
     VAR5=`echo $LINE | awk '{ print $5 }'`
     echo ${VAR1}"   "${VAR2}"   "${VAR3}"   "${VAR4}"   "${VAR5}
done < input_file

Could someone help please.

Moderator's Comments:
Mod Comment Use code tags please, ty.

Last edited by zaxxon; 08-16-2010 at 10:17 AM..
# 2  
Old 08-16-2010
After applying code tags to your snippets, I only see 3 or 4 columns/fields per line. It might help if you use some kind of field separator for every line so that you have a consistent number of fields per row. That would make parsing much easier, if this is possible.

Example:
Code:
X01;1;JOE;20100312
X02;;TOD;20100312
X03;3;SAM;20100312

I took a semicolon as delimeter. Where there is an empty column/field, you just have 2 ;; adjacent to each other, like in a csv file. With having just <n> of blanks between them, it is not easy to check which of the fields is empty and the wrong variable might be assigned.
# 3  
Old 08-16-2010
Thanks zaxxon. The output that I am trying to parse is from sql command. If I apply semicolon as a delimiter, it should be like this:
Code:
X01;1; ;JOE;20100312
X02; ; ;TOD;20100312
X03; ;3;SAM;20100312

This is why I am trying to read a location using cut command, instead of reading a field.
# 4  
Old 08-16-2010
cut -d can take ; as a delimeter for example and any other character you like. awk can even take multiple delimeters.
Don't remove delimeters as they are very helpful.

Maybe this is ok for you:
Code:
IFS=";"

while read A B C D E; do
   echo "A=$A"
   echo "B=$B"
   echo "C=$C"
   echo "D=$D"
   echo "E=$E"
        echo "---- processing next line ----"
done < infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Cut command: can't make it cut fields

I'm a complete beginner in UNIX (and not a computer science student either), just undergoing a tutoring course. Trying to replicate the instructions on my own I directed output of the ls listing command (lists all files of my home directory ) to My_dir.tsv file (see the screenshot) to make use of... (9 Replies)
Discussion started by: scrutinizerix
9 Replies

2. UNIX for Dummies Questions & Answers

Cut pid from ps using cut command

hay i am trying to get JUST the PID from the ps command. my command line is: ps -ef | grep "mintty" | cut -d' ' -f2 but i get an empty line. i assume that the delimiter is not just one space character, but can't figure out what should i do in order to do that. i know i can use awk or cut... (8 Replies)
Discussion started by: ran ber
8 Replies

3. Shell Programming and Scripting

Cut Command error cut: Bad range

Hi Can anyone what I am doing wrong while using cut command. for f in *.log do logfilename=$f Log "Log file Name: $logfilename" logfile1=`basename $logfilename .log` flength=${#logfile1} Log "file length $flength" from_length=$(($flength - 15)) Log "from... (2 Replies)
Discussion started by: dgmm
2 Replies

4. UNIX for Dummies Questions & Answers

more f skips two pages

Hi, I am new to linux. I am using more command to view the contents of a file. If the file has many pages i am using f to move forward to the next page. But when i press f it skips to two pages instead of one page. i checked the man more. It shows the default is 1. Please share your... (1 Reply)
Discussion started by: nokiak810
1 Replies

5. Shell Programming and Scripting

Remote script skips "read" command

This script is supposed to display a file ( crontab ), ask the user if they wish to update the file, then it goes through an update routine. #!/bin/bash FILE=/etc/crontab tail -5 $FILE echo -n "Does crontab need updating" read HOURS ...routines ....etc... Runs locally... (8 Replies)
Discussion started by: Bubnoff
8 Replies

6. Shell Programming and Scripting

Include white spaces while using CUT command

Hi I tried to extract 19 characters (default) enclosed with in tag from a file using cut command. If the characters comprises of double space, the cut command gives the output with a single spacing. file 1 <name>Kumar Rajasekaran</name> cut -c7-26 "file1" the out put i received is ... (48 Replies)
Discussion started by: Sekar1
48 Replies

7. Shell Programming and Scripting

wget skips certain files.

I am trying to use wget to automate downloading of some mp3/wav files. However, I can't get it to follow the link to the mp3s. This is the line (it is not really the website): wget -prl 1 http://website.com/alarmsHowever, if I right-click and copy the link on the webpage in firefox, then... (4 Replies)
Discussion started by: Narnie
4 Replies

8. UNIX for Dummies Questions & Answers

Removing Double Spaces for cut command

Hi I have a shell script that looks for running processes in order to kill them. The script (ksh) gets the PID of these processes using the following: PROCS=`ps -fu ${USERID} | egrep "MONITOR" | grep -v grep | cut -d" " -f4` However, I spotted an issue where PID's of different lengths... (3 Replies)
Discussion started by: mikem22
3 Replies

9. Shell Programming and Scripting

no count of spaces in cut

i want to give numbers to cut without worrying about the spaces; echo "12 345 6 78 9" | cut -c 1-9 echo "123 456 789" | cut -c 1-9 echo "1 2 3 4 5 6 7 8 9" | cut -c 1-9 the output of these three must be allways; 123456789 is that posible? (6 Replies)
Discussion started by: Tártaro
6 Replies

10. HP-UX

Cron Skips Execution

Hi All, I got a very strange problem, i have created a cron schedule that will run a programme once in every 12 mins, like 04,16,28,40,52 * * * * /myf/startProcessA 1> &- Some time the cron skips the execution such as 04,28 instead of executing at 16 mins. it starts execution at 28, i... (6 Replies)
Discussion started by: nag_sundaram
6 Replies
Login or Register to Ask a Question