Visit Our UNIX and Linux User Community


Advice using cut & echo combination commands


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Advice using cut & echo combination commands
# 1  
Old 07-27-2010
Question Advice using cut & echo combination commands

Hi,
I am cutting data from a fixed length test file and then writing out a new record using the echo command, the problem I have is how to stop multiple spaces from being written to the output file as a single space.
Example:
Code:
cat filea | while read line
do
  field1=`echo $line | cut -c1-2`
  field2=`echo $line | cut -c10-20`
  field3=`echo $line | cut -c21-35`
  echo $field1$field2$field3 >>outfile
done

Both field2 & field3 contain variable length text, the unused portion being space filled.

Thanks
Dave

Last edited by dc18; 07-27-2010 at 06:47 PM.. Reason: Please indent your code and use code tags
# 2  
Old 07-27-2010
Hi,

Post your sample input file and expected output.
# 3  
Old 07-27-2010
file in / file out

Hi,
If input record is
Code:
 
123456789EXAMPLE    DESCRIPTION    1234

I want
Code:
12EXAMPLE    DESCRIPTION

(with 3 spaces betwee EXAMPLE & DESCRIPTION & 4 spaces at end)

but I get
Code:
12EXAMPLE DESCRIPTION

(with 1 space between EXAMPLE & DESCRIPTION & 1 space at end)

ie multiple spaces are automatically reduced to 1 space !

Last edited by vgersh99; 07-27-2010 at 06:56 PM.. Reason: code tags, please!
# 4  
Old 07-27-2010
Code:
nawk '{print substr($0,1,2) substr($0,10,10) substr($0, 21,14)}' filea > outfile

This User Gave Thanks to vgersh99 For This Post:
# 5  
Old 07-27-2010
MySQL

Quote:
Originally Posted by dc18
the problem I have is how to stop multiple spaces from being written to the output file as a single space.
Just add quotes to the last echo
Code:
 echo "$field1$field2$field3" >>outfile

This User Gave Thanks to Aia For This Post:
# 6  
Old 07-27-2010
Code:
#!/bin/bash
while read -r LINE
do
  echo "${LINE:0:2}${LINE:9:10}${LINE:20:35}"
done <"file"

These 2 Users Gave Thanks to kurumi For This Post:
# 7  
Old 07-27-2010
reserve the spaces before and after DESCRIPTION.

Code:
echo "123456789EXAMPLE    DESCRIPTION    1234" |cut -c1-2,10-35

12EXAMPLE    DESCRIPTION

This User Gave Thanks to rdcwayx For This Post:

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Optimize a combination of commands

Im currently running this command to satisfy a particular task. it works for my purposes. but i want to be able to optimize this string of commands and have it be reduced to 1 or 2 commands, if at all possible: head -4 datafile 2>/dev/null | cut -c1-400 | wc | awk '{print $2$1$3}' (5 Replies)
Discussion started by: SkySmart
5 Replies

2. Shell Programming and Scripting

Using a combination of sort/cut/grep/awk/join/paste/sed

I have a file and need to only select users that have a shell of “/bin/bash” in the line using awk or sed please help (4 Replies)
Discussion started by: boyboy1212
4 Replies

3. Shell Programming and Scripting

combination between || and && in IF condition with ksh

Dear All, Please advice about this issue. when i run this line in a script if && || && || && if i enter $x = test3 and $y = test1 the If condition apply while it should not Best Regards (2 Replies)
Discussion started by: islam.said
2 Replies

4. Shell Programming and Scripting

Append && echo "success" to all commands

I am learning to build from SVN and other tools, with a lot of copying and pasting from forums. I like to append && echo "success" to all commands so that I can see at a glance if things went all right. Is there a way that I can have the bash shell append this to all commands? Thanks! (5 Replies)
Discussion started by: dotancohen
5 Replies

5. Shell Programming and Scripting

storing output from echo & cut into variable

Hi All, Hope someone can advise here as I have been struggling to find a syntax that works here. I have tried a stack of combination I have seed in the forums but I think because I have needed to use "" and `` in the statments another method is found. I am reading in lines with the following... (1 Reply)
Discussion started by: nkwilliams
1 Replies

6. Shell Programming and Scripting

Sed Awk Cut Grep Combination Help ?

I have been reading for a few hours trying to educate myself enough to accomplish this task, so please know I have performed some research. Unfortunately, I am not a *NIX scripting expert, or a coder. I come from a network background instead. SO, here is my desired outcome. I have some Cisco... (5 Replies)
Discussion started by: abbzer0
5 Replies

7. UNIX for Dummies Questions & Answers

Combination of find -xargs & wc -l

Dear all, I have to calculate sum of record count of files of the specified directory. First I tried the following way which prints one or more outputs. How can I sum of this output? find /home/work/tmp/1/O/ -type f -print0 | xargs -0 wc -l | grep total 1666288 total 1073908 total ... (4 Replies)
Discussion started by: mr_bold
4 Replies

8. UNIX for Dummies Questions & Answers

combination of two commands

I want to show a output like this Lee Ballancore PID TTY TIME CMD 31799 pts/3 00:00:00 vim 31866 pts/3 00:00:00 vim 2495 pts/7 00:00:00 vim 8368 pts/0 00:00:00 vim 9544 pts/2 00:00:00 ps Alistairr Rutherford PID TTY TIME CMD 8368 pts/0 00:00:00 vim 9544 pts/2 00:00:00 ps ... (3 Replies)
Discussion started by: nehaquick
3 Replies

9. UNIX for Dummies Questions & Answers

Combination Of commands

Hello All, I just wanted to know what are the different ways of using commands in combination. The most common one which i know is using pipes. Also grouping is also done like ( ls; date) where output of both the commands is displayed. Are there any other ways of combining various... (2 Replies)
Discussion started by: rahulrathod
2 Replies

Featured Tech Videos