Find number of characters in a column and replace


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find number of characters in a column and replace
# 1  
Old 05-07-2013
Find number of characters in a column and replace

Hi all,

I want to count total no. of characters in a column. and if no. of charaters are more than 3 then it must replace it by splitted string. ie, it must place a space after 3 characters.

Ex:
Code:
21 435g asd3dd jklfjwe wer

column number 3 has 4 alphanumeric character, so it must be splitted like:
Code:
21 435g asd 3dd jklfjwe wer

Thanks in advance.Smilie
# 2  
Old 05-07-2013
What have you tried so far?
# 3  
Old 05-07-2013
I am still in learning phase of linux.....
I have tried "length" command to count the characters of a string.
then I tried to use "cut" and "fold" commands but cant corelate them.
This is not my assignment task.....I need to learn this for other purpose. So, kindly help me out in this.
# 4  
Old 05-07-2013
I noticed some inconsistencies in your requirement!

If you want to split each alpha-numeric string greater than 3 character length, then why the following highlighted 4 character length string is not considered?
Code:
21 435g asd 3dd jklfjwe wer

By the way if your requirement is to split strings that are greater than 4 character length, then try this code:
Code:
awk '
        {
                for ( i = 1; i <= NF; i++ )
                {
                        if ( length ($i) > 4 )
                        {
                                if ( $i ~ /[a-zA-Z]/ && $i ~ /[0-9]/ )
                                {
                                        gsub( /.../, "& ", $i )
                                        sub(" $", X, $i )
                                }
                        }
                }
        }
1 ' file

This User Gave Thanks to Yoda For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to print column number while ignoring alpha characters

I have the following script that will print column 4 ("25") when column 1 contains "123". However, I need to ignore the alpha characters that are contained in the input file. If I were to ignore the characters my output would be column 3. What is the best way to print my column of interest... (3 Replies)
Discussion started by: ncwxpanther
3 Replies

2. Shell Programming and Scripting

Find/replace alpha characters in string

Hi, I would like to find a 3-letter character series in a string/variable and replace it with x's. An example set of strings is: 563MS333_101_afp_400-100_screening 563MS333_104-525_rjk_525_screening 563MS333_110_dlj_500-100_w24 563MS333_888-100_mmm_424_screening The only constants... (5 Replies)
Discussion started by: goodbenito
5 Replies

3. Shell Programming and Scripting

Find and replace with 0 for characters in a specific position

Need command for position based replace: I need a command to replace with 0 for characters in the positions 11 to 20 to all the lines starts with 6 in a file. For example the file ABC.txt has: abcdefghijklmnopqrstuvwxyz 6abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyz... (4 Replies)
Discussion started by: thangabalu
4 Replies

4. Shell Programming and Scripting

Replace column by random number addition

Here is my problem:- I have a file with pipe separated values. CR|20121021|079|ABC|N|DLS|00038|DLS|04750|1330597704|634234|634|0 CR|20121021|079|ABC|N|DLS|00038|DLS|05118|2071690102|354|351|3 CR|20121021|079|ABC|N|DLS|00038|DLS|05140|960051505|1088|1088|0... (4 Replies)
Discussion started by: Yoda
4 Replies

5. Shell Programming and Scripting

Count number of characters in particular column

Hi i have data like abchd 124 ldskc aattggcc each separated by tab space i want to count number of characters in 4th column and print it in new column with tabspace for every line can anyone help me how to do it. Thanks. (3 Replies)
Discussion started by: bhargavpbk88
3 Replies

6. Shell Programming and Scripting

find replace a pattern and following characters in a word

Suppose that I have a string "one:#red two:#yellow three:#gr'een four:#blu^e" and I want to replace the pattern :# and the following characters in the word with nothing. The output string should look "one two three four" How can I do this with sed. Some points to consider (a) the last word in... (1 Reply)
Discussion started by: superuser84
1 Replies

7. UNIX for Dummies Questions & Answers

reducing the number of characters in a column

Hi, I would like to take the first column of a bunch of lines and take only the 6th through 15th characters. The first column are not regular. gbAY277147.1Ptv3.T1469 CTTGAACAT gbAY277149.1Ptro3.T1469 CTTGAACAT gbAY287891.1Hs3.T1469 CTTGAACATTTGC into 7147.1Ptv3 CTTGAACAT... (4 Replies)
Discussion started by: mikey11415
4 Replies

8. UNIX for Dummies Questions & Answers

replace all numbers in column with another number in bash

Hi, I've been trying to replace the numbers in the first column of my text file with all ones, unless the number is equal to 8. I have this: 1 1 11 123 258 2 1 9 135 175 1 1 15 143 274 8 1 13 153 172 8 1 13 154 166 8 1 13 154 167 3 1 15 237 255 4 1 15 243 202 1 1 13 133 166... (4 Replies)
Discussion started by: goodbenito
4 Replies

9. UNIX for Dummies Questions & Answers

Find and replace special characters in a file

HI All I need a shell script ehich removes all special characters from file and converts the file to UTF-* format Specail characters to be removed must be configurable. strIllegal = @"?/><,:;""'{|\\+=-)(*&^%$#@!~`"; Please help me in getting this script as my scripting skilla are... (2 Replies)
Discussion started by: sujithchandra
2 Replies

10. UNIX for Dummies Questions & Answers

Help with find and replace w/string containing special characters

Can I get some help on this please, I have looked at the many post with similar questions and have tried the solutions and they are not working for my scenario which is: I have a text file (myfile) that contains b_log=$g_log/FILENAME.log echo "Begin processing file FILENAME " >> $b_log ... (4 Replies)
Discussion started by: CAGIRL
4 Replies
Login or Register to Ask a Question