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
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

cut -f 1-4 -d , $file > file.tmp
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,

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

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

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);
				System.out.println("string buff val is"+stringBuffer.length());
				System.out.println("string buff val is"+stringBuffer.toString());
		} catch (FileNotFoundException e) {
		} catch (IOException e) {
		} finally {
			if (br != null) {
				try {
				} catch (IOException e) {
	  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"));	        	
	    		System.out.println("bufferedWriter obj is" + bufferedWriter);
	    		} catch (Exception ex) {
	    			System.out.println("exception is thrown while writing to the file " + ex);
	    		} finally {
	    			// close

Open in new window

inputfile(sri.txt) contains the values:

output file(sri2.txt):
echo value1,,value3,value4 | sed -e s/,,/,X,/

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bjeAuthor Commented:
Thank you for all of the solutions.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.