pull columns from a file and put into a new file

Not sure the best way to do this.  Need to take the first 4 fields of a file that is comma separated and put them into a new file.  The file may have 30 lines or 300 lines.  Need to read through each line.

Would I use a for loop to do this or a read command

count the number of lines in the file.

numberoflines=`wc -l  $file'

for $numberoflines in
 cut -c 1-4  > file.tmp

or would read be better.  not sure how this would be structured.

Thanks for the help
bjeAsked:
Who is Participating?
 
ozoCommented:
echo value1,,value3,value4 | sed -e s/,,/,X,/
value1,X,value3,value4
0
 
ozoCommented:
cut -f 1-4 -d , $file > file.tmp
0
 
bjeAuthor Commented:
thanks for the answer.

I am replace the second field with a value.  Using sed and do have the syntax right

file has,
value1,,value3,value4

would like to replace the second field which has no value with a value

sed -e s/\" "/\"X"/g filename > newfilename

thanks
0
 
srikoteshCommented:
Try this program:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
	 
	public class FileReaderSeparatedWithComma {	
	 
	  public static void main(String[] args) {		
	 
		String csvFile = "G:\\JAVA\\sri.txt";
		BufferedReader br = null;
		String line = "";
		String cvsSplitBy = ",";
		StringBuffer stringBuffer = new StringBuffer();
		BufferedWriter bufferedWriter = null;
		String listByLineOutPutValue = "";
	 
		try {
	 
			br = new BufferedReader(new FileReader(csvFile));
			while ((line = br.readLine()) != null) {
	 
			        // use comma as separator
				String[] fields = line.split(cvsSplitBy);
				
	 
				System.out.println(fields[0]+ fields[1] +fields[2]+ fields[3]);
				String ConcatOfFour = fields[0]+ fields[1] +fields[2]+ fields[3];
				System.out.println("string val is"+ConcatOfFour);
				stringBuffer.append(ConcatOfFour);
				stringBuffer.append(System.getProperty("line.separator"));
				System.out.println("string buff val is"+stringBuffer.length());
				System.out.println("string buff val is"+stringBuffer.toString());
				writeDataToTextFile(bufferedWriter,stringBuffer,listByLineOutPutValue);
			}
	 
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	 
		System.out.println("Done");
	  }
	  public static void writeDataToTextFile(BufferedWriter bufferedWriter, StringBuffer stringBuffer,String listByLineOutPutValue) throws IOException{
		  listByLineOutPutValue = stringBuffer.toString();
			System.out.println("listByLine *****" + listByLineOutPutValue);
			Writer writer = null;
	        try {
	        	bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("G:\\JAVA\\sri2.txt"), "utf-8"));	        	
	    		bufferedWriter.write(listByLineOutPutValue);
	    		System.out.println("bufferedWriter obj is" + bufferedWriter);
	    		} catch (Exception ex) {
	    			System.out.println("exception is thrown while writing to the file " + ex);
	    		} finally {
	    			// close
	    			bufferedWriter.close();
	    		}
			
		  
		  
	  }
	 
	}

Open in new window


inputfile(sri.txt) contains the values:
"1.0.0.0","1.0.0.255","16777216","16777471","AU","Australia"
"1.0.1.0","1.0.3.255","16777472","16778239","CN","China"

output file(sri2.txt):
"1.0.0.0""1.0.0.255""16777216""16777471"
"1.0.1.0""1.0.3.255""16777472""16778239"
0
 
bjeAuthor Commented:
Thank you for all of the solutions.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.