filling a character


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting filling a character
# 1  
Old 10-29-2010
Data filling a character

in my file data is like this

Code:
1,2,3
3,4,5,6,7,8
10,11,23,24

i want to make as

Code:
1,2,3,?,?,?
3,4,5,6,7,8
10,11,23,24,?,?


here max no of words(separated by comma) in a line is 6.so every line contains 6 words.Line which have less than 6 words replaced with '?' as a word


i have 50,000 lines in my file.maximum words in a line are 156 so i need to update all 50,000 lines....help me guys
thanks in advance

Last edited by radoulov; 10-29-2010 at 10:25 AM.. Reason: Code tags, please!
# 2  
Old 10-29-2010
Code:
awk -F, 'END {
  for (i = 0; ++i <= NR;) {
    n = split(r[i], t)
    for (j = 0; ++j <= m;)
      printf "%s", (t[j] ? t[j] : "?") (j < m ? OFS : RS)
    }
  }
{
  r[NR] = $0; NF > m && m = NF
  }' OFS=, infile

You may need to modify the code, if for your implementation NR is not available in the END block.


Perl:

Code:
perl -F, -lane'
  push @r, [@F]; @F > $m and $m = @F;
  END {
    for $l (@r) {
	print join ",", 
	  map $$l[$_] || "?", 0 .. $m - 1
    } 
  }' infile


Last edited by radoulov; 10-29-2010 at 10:58 AM.. Reason: Perl version added.
# 3  
Old 10-29-2010
Another approach:
Code:
awk -F, 'NR==FNR{ nf=nf<NF?NF:nf; next } {
  for(i=NF+1;i<=nf;i++){$0=$0 ",?"}
}
1' file file

# 4  
Old 10-29-2010
Code:
{ IFS="," ; while read a b c d e f ; do echo "${a:=?},${b:=?},${c:=?},${d:=?},${e:=?},${f:=?}" ; done }<input

Code:
# cat infile
1,2,3
3,4,5,6,7,8
10,11,23,24

# { IFS="," ; while read a b c d e f ; do echo "${a:=?},${b:=?},${c:=?},${d:=?},${e:=?},${f:=?}" ; done }<infile
1,2,3,?,?,?
3,4,5,6,7,8
10,11,23,24,?,?
?,?,?,?,?,?
#


Last edited by ctsgnb; 10-29-2010 at 11:31 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Filling empty cells

How do you fill empty cells that do not have any data in them with "X" in a tab delimited text file? Thanks! (4 Replies)
Discussion started by: evelibertine
4 Replies

2. Shell Programming and Scripting

/tmp filling up

Does anyone know of a way to redirect the ksh default of processing data in /tmp to another file system or / something else? My ksh script is parsing large DB files and it keeps filling up /tmp on the root disk. I have a 1 Tb disk with most of its space. How do I re-direct the /tmp ksh... (6 Replies)
Discussion started by: cchelten
6 Replies

3. Solaris

Root partition filling up

I have a T1000 Sparc server that has a relatively small root partition which is 24Gb and a larger partition dedicated to /export/home that is approximately 100 Gb. We have a lot of data going to /var/audit and to /var/core/corefiles. Is there any non-destructive way to redirect files from... (4 Replies)
Discussion started by: goose25
4 Replies

4. UNIX for Dummies Questions & Answers

Need help filling in ranges

I have a list of about 200,000 lines in a text file that look like this: 1 1 120 1 80 200 1 150 270 5 50 170 5 100 220 5 300 420 The first column is an identifier, the next 2 columns are a range (always 120 value range) I'm trying fill in the values of those ranges, and remove... (4 Replies)
Discussion started by: knott76
4 Replies

5. Shell Programming and Scripting

Filling positions based on consensus character

I have files with hundreds of sequences with missing characters represented by a dash ("-"), something like this I need to go sequence by sequence and if a dash is found, it should be replaced with the most common character in that particular position. Thus, in my example the dash in the second... (6 Replies)
Discussion started by: Xterra
6 Replies

6. UNIX for Dummies Questions & Answers

Root filesystem filling up!

Hi all. New to the forum and new to Unix admin... / filesystem filled up and I can't find where the large files are. Any help will be apppreciated: # df -k Filesystem kbytes used avail capacity Mounted on /dev/dsk/c1t0d0s0 8063580 7941745 41200 100% / /proc ... (4 Replies)
Discussion started by: jamie_collins
4 Replies

7. Shell Programming and Scripting

Filling in missing columns

Hi all, I have a file that contains about 1000 rows and 800 columns. Nearly every row has 800 columns but some DONT. I want to extend the rows that dont have values with NA's. Here is an example: my file bob 2 4 5 6 8 9 4 5 tar 2 4 5 4 3 2 9 1 bro 3 5 3 4 yar 2 ... (7 Replies)
Discussion started by: gisele_l
7 Replies

8. UNIX for Dummies Questions & Answers

filling variable with ls

hello All, Need some further help. This will make my live easier. Instead of copy and pasting I think I can automate some website building. when I do a ls from a directory I need the file names placed into a sentence. it is going about wordts like: word-AB-1234.jpg... (1 Reply)
Discussion started by: ToXiQ
1 Replies

9. Shell Programming and Scripting

Filling out a text

Hello, I have a problem with filling out a text. I have different lenghts in a file and would like to see that all the lines becomes the same length by putting a zero in front off the line. Please advice. Old File: ---------- 5432 233 3455 4432 New File: ----------- 5432 0233... (6 Replies)
Discussion started by: peterk
6 Replies

10. Solaris

Filesystem filling up and no clue as to why!

df shows that the filesystem is filling up and the usage is 94%. However when I actually traverse to the directory I du shows only about 10% of the space occupied! Below is the output of df and du: >>>df -kh /cbmdata/00 470M 393M 29M 94% /cbmdata/00 >>>/cbmdata/00>... (3 Replies)
Discussion started by: zombiezparadize
3 Replies
Login or Register to Ask a Question