如何从文件中添加几行到另一个文件的开头?

时间:2015-08-10 01:06:07

标签: unix sed

我使用sed从文件中提取前69行。

sed -n '1,69p' file.vcf > header.vcf

如何将它放在另一个文件的开头(拳头线)?

3 个答案:

答案 0 :(得分:1)

文件{ head -n69 file.vcf; cat other.vcf; } >tmp$$ && mv tmp$$ other.vcf 开头的69行的一种方法是:

head -n69 file.vcf

file.vcf输出cat other.vcf的前69行。 other.vcf输出整个>tmp$$&& mv tmp$$ other.vcf在临时文件中捕获该输出。如果临时文件的创建成功,other.vcf会导致com.teacherapp.adapter; import java.util.List; import android.annotation.SuppressLint; import android.app.Activity; import android.app.AlertDialog; import android.content.Context; import android.graphics.Bitmap; import android.text.Editable; import android.text.TextWatcher; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.CheckBox; import android.widget.EditText; import android.widget.ImageView; import android.widget.TextView; import com.koushikdutta.urlimageviewhelper.UrlImageViewCallback; import com.koushikdutta.urlimageviewhelper.UrlImageViewHelper; import com.mikhaellopez.circularimageview.CircularImageView; import com.teacherapp.async.GetMarksAsync; import com.teacherapp.bean.TeacherBean; import com.teacherapp.fragments.MarkeUpdateFragment; import com.teacherapp.utility.ConstantTeacher; import com.wemited.schoolfly.teacherapp.R; public class MarksListAdaptor extends BaseAdapter { List<TeacherBean> recentitems; LayoutInflater inflate; TeacherBean item; AlertDialog alertDialog, fpDialog; public static final String KEY_SETTING = "setting"; public static CheckBox check_img; Context context; @SuppressWarnings("unused") private boolean isChecking = true; @SuppressWarnings("unused") private int mCheckedId = R.id.type1; public static String status_all; int position1; //String marks; public MarksListAdaptor(Activity context, List<TeacherBean> recentitems, String string) { this.recentitems = recentitems; inflate = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); this.context = context; status_all = string; } @Override public int getCount() { return recentitems.size(); } @Override public Object getItem(int position) { return recentitems.get(position); } @Override public long getItemId(int position) { return position; } public static class ViewHolder { public TextView txt_name, txt_father; public CircularImageView logo_img; public EditText edt_marks, edt_grad; } @SuppressLint("InflateParams") @SuppressWarnings("unused") @Override public View getView(final int position, View convertView, ViewGroup parent) { item = recentitems.get(position); final TeacherBean item1 = (TeacherBean) getItem(position); View myview = convertView; if (convertView == null) { myview = inflate.inflate(R.layout.stud_mark_list_item, null); } final ViewHolder holder = new ViewHolder(); holder.txt_name = (TextView) myview.findViewById(R.id.txt_name); holder.txt_father = (TextView) myview.findViewById(R.id.txt_father); holder.logo_img = (CircularImageView) myview.findViewById(R.id.img); holder.edt_marks = (EditText) myview.findViewById(R.id.edt_marks); holder.edt_grad = (EditText) myview.findViewById(R.id.edt_grad); holder.txt_name.setText("" + item.getStudent_name()); holder.txt_father.setText("" + item.getStudent_roll_no()); if (status_all.equals("0")) { holder.edt_marks.setVisibility(View.INVISIBLE); holder.edt_grad.setVisibility(View.VISIBLE); } else { holder.edt_grad.setVisibility(View.INVISIBLE); holder.edt_marks.setVisibility(View.VISIBLE); } String logo = ConstantTeacher.img_base + item.getStudent_img(); holder.logo_img.setAnimation(null); UrlImageViewHelper.setUrlDrawable(holder.logo_img, logo, R.drawable.ic_launcher, new UrlImageViewCallback() { @Override public void onLoaded(ImageView imageView, Bitmap loadedBitmap, String url, boolean loadedFromCache) { if (!loadedFromCache) { } } }); holder.edt_marks.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { String marks = holder.edt_marks.getText().toString(); GetMarksAsync.status_list.set(position, marks); } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void afterTextChanged(Editable s) { } }); holder.edt_grad.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { String grad = holder.edt_grad.getText().toString(); System.out.println("marks"+grad); GetMarksAsync.status_list.set(position, grad); } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void afterTextChanged(Editable s) { } }); return myview; } } 替换为新版本。

答案 1 :(得分:1)

这可能适合你(GNU sed):

sed -i -e '69r other.vcf' -e '69q' header.vcf

使用-i标志编辑输入文件。在第69行附加other.vcf并退出输入文件处理。

N.B。 other.vcf附加在第69行之后,第一个命令必须通过换行符或第一个-e标志的单独调用与第一个命令分开。

sed -i $'69{r other_file\n;q}' file

如果bash是shell,也会有效。

答案 2 :(得分:0)

试试这个:

sed -n'1,69p'file.vcf&gt; header.vcf&amp;&amp; cat header.vcf otherfile&gt; / tmp / abcd&amp;&amp; mv / tmp / abcd otherfile