Get rid of the 7th character of each line if this is a space


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Get rid of the 7th character of each line if this is a space
# 1  
Old 11-24-2009
Get rid of the 7th character of each line if this is a space

I have a text file like this

Code:
...
     B 16   1.340E+05
     A 18   3.083E+02
   Wu123  1.365E+02
...

I would like to get rid of the 7th character of each line if this is a space character.

Thank you,
Sarah
# 2  
Old 11-24-2009
bash
Code:
while IFS= read -r line
do
    case "${line:6:1}" in
    " ") 
            echo "blank at 7th char in line: $line" 
            echo "blank removed: ${line:0:6}${line:7}"
            ;;
    *) echo "$line";;
    esac    
done <"file"

# 3  
Old 11-24-2009
Quote:
Originally Posted by ghostdog74
bash
Code:
while IFS= read -r line
do
    case "${line:6:1}" in
    " ") 
            echo "blank at 7th char in line: $line" 
            echo "blank removed: ${line:0:6}${line:7}"
            ;;
    *) echo "$line";;
    esac    
done <"file"

Thank you
# 4  
Old 11-24-2009
Code:
sed 's/\([[:print:]]\{6\}\) \(\.*\)/\1\2/g' file

# 5  
Old 11-24-2009
Code:
$ awk 'BEGIN {FS=OFS=""} {if($7==" ") {$7=""}}1' urfile
     B16   1.340E+05
     A18   3.083E+02
   Wu123  1.365E+02


Last edited by rdcwayx; 11-24-2009 at 07:49 PM..
# 6  
Old 12-18-2009
The solution with awk is the fastest. Thank you.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk or other way to find out number of occurrence of 7th character

Hi all, I am looking for to filter out based on 7th character and list the number of occurrence based on the 7th character if p , d , o or m 1. if 7th character is p , Output should be: p_hosts = N 2. if 7th character is d , Output should be: d_hosts = N 3. if 7th character is o , Output... (10 Replies)
Discussion started by: rveri
10 Replies

2. Shell Programming and Scripting

How to add a character after the first word/space on each line?

Hi:) I have a large file ( couple thousand lines ) and I'm trying to add a character after the first word/space on each line. eg: First line of text Second line of text Third line of text Fourth line of text Fifth line of text I'd like to accomplish: First - line of text Second... (8 Replies)
Discussion started by: martinsmith
8 Replies

3. Shell Programming and Scripting

Read character by character in line in which space is also included

Hi friend, I have one file , and i want to read that file character by character. I need this script in ksh. while using read option with -n1 am getting error. while read -n1 c read has bad option And if i am using below script, then if in a line has space like this ( Pallvi mahajan)... (10 Replies)
Discussion started by: pallvi_mahajan
10 Replies

4. Shell Programming and Scripting

Remove new line character and add space to convert into fixed width file

I have a file with different record length. The file as to be converted into fixed length by appending spaces at the end of record. The length should be calculated based on the record with maximum length in the file. If the length is less than the max length, the spaces should be appended... (4 Replies)
Discussion started by: Amrutha24
4 Replies

5. Shell Programming and Scripting

In Sed how can I replace starting from the 7th character to the 15th character.

Hi All, Was wondering how I can do the following.... I have a String as follows "ACCTRL000005022RRWDKKEEDKDD...." This string can be in a file called tail.out or in a Variable called $VAR2 Now I have another variable called $VAR1="000004785" (9 bytes long), I need the content of... (5 Replies)
Discussion started by: mohullah
5 Replies

6. Shell Programming and Scripting

awk get rid of space in end of field

Hello. I'm using a file to "grep" in a 2nd one (with awk) cat file1 2 first user 9 second user 1 third user (with a space after user) I want to get the line except the 1st field so I do : field=$(gawk '{$1 =""; print $0}' file | sed 's/^ //') It works but it deletes... (5 Replies)
Discussion started by: xanthos
5 Replies

7. Shell Programming and Scripting

A script that read specific fileds from the 7th line in a file

Dear All, I need a unix script that will read the 7th line and especially these fileds from a file Mo speed 16, Mt speed 15 every 15 minutes starting from 00:00 to 23:45 on daily basis and put the result in a txt file and name it MT_MO_20090225.txt, please also note that the system date format... (2 Replies)
Discussion started by: samura
2 Replies

8. Shell Programming and Scripting

How can I get rid of the ` character from input file?

A sed command works most of the time however it fails sometimes. I put each line (record) I read of a file through the following command data=$(cat file | sed 's///g' | sed 's|.*ex:Msg\(.*\)ex:Msg.*|\1|' ) When I run the script I get a message that states that there is an invalid format... (6 Replies)
Discussion started by: gugs
6 Replies

9. UNIX for Advanced & Expert Users

Get rid of junk character in a file

I have a file with one of the following lines, when opened with vi 33560010686GPT£120600GBPGBP10082007DS In the above line, I want to get rid of the junk character before the £ (pound sysmbol). When I tried copying £ from windows and copy in unix vi, it prints as £ and I tried pattern replace... (2 Replies)
Discussion started by: nskworld
2 Replies

10. Shell Programming and Scripting

How to get rid of last line

I have to process a data file in Ab Initio. This data file is pipe delimited. BUt the file may have a Disclaimer line at the end. So before picking it for processing, I need to check if this line is there I need to remove it. ANy suggestions. Thanks Shalu (1 Reply)
Discussion started by: shalua
1 Replies
Login or Register to Ask a Question