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?

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

x
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.

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
ozoCommented:
echo value1,,value3,value4 | sed -e s/,,/,X,/
value1,X,value3,value4
0

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.
0
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.